nullSSD1: Introduction to Information SystemsSSD1: Introduction to Information SystemsUnit 2: Introduction to Java and Object-Oriented Programming
Liwei WANG liwei.wang@whu.edu.cn ISS, Wuhan University2.1 Programming with Java2.1 Programming with JavaProgramming with Objects
Java Program Development
First Look at Java
Element of a Java Servlet
Planning Servlet Development
Guidelines for Java Development2.1.4 Elements of a Java Servlet2.1.4 Elements of a Java ServletThe Welcome Servlet
More Servlets
Getting User Input
Debugging a ServletnullnullnullImport
public
class
extends
void (无效的)
throws
KeywordsIdentifiers(标识符)Welcome
HttpServlet
doPost
HttpServletRequest
HttpServletResponse
PrintWriter
ServletException
IOException
request
response
out
setContentType
getWriter
println Identifiers(标识符)nullImport all the clssses contained in java.io, which are required by the servlet to do any input or output tasknullThe description for the Welcome class.
the servlet receives a doPost message and response it. null使用setContentType设定servlet生成的文件的格式:
“text/plain”表示生成一个
纯文本文件;
“text/html” 表示生成一个
HTML Web网页;
“ text/gif”表示生成一个
GIF文件. Sending setContentType message to response object (invoking method) nullsend the getWriter message to the response object nullprintln message requires one resource actual text to execute, More ServletsWriting servlets that send a Web page to the browser.
Writing servlets that send an image instead of displaying a welcome message .
Example: HtmlWelcome servlet
参见在线教材2.1.4节的Listing 2
More ServletsGetting User InputWhile the form in MyWelcomeForm.html allowed the user to enter a name, the message sent by the Welcome servlet had nothing to do with the name entered by the user.
This is because the source code for the Welcome servlet we examined did not attempt to find out what the user entered and to include that information in the Web page sent to the browser. Getting User InputGetting User InputSave the MyWelcomeForm.java as PersonalWelcome.java and modify it to get user’s input parameter.
String userName = request.getParameter("Name");
Compile the source file and modify the form correspond.
Reload the class file and html file to debug.Getting User Inputnullsend the getParameter message to request objectDebugging a Servlet(调试)Adding System.out.println statements in servlet source code
System.out.println(“Debugging information");
参见2.1.4节listing 4的Welcom.java
Information about how far the servlet execution has progressed is displayed in the console window of the workbench.
在servlet运行时,可以在workbench的窗口中看到调试提示信息。Debugging a Servlet(调试)nullSystem类是一些与系统相关属性和方法的集合
System类是一个特殊类,它是一个公共最终类,不能被继承,也不能被实例化,即不能创建System类的对象。
System.out is an instance of PrintStream class.nullServlet运行时,调试提示信息显示在workbench的信息提示窗口2.1 Programming with Java2.1 Programming with JavaProgramming with Objects
Java Program Development
First Look at Java
Element of a Java Servlet
Planning Servlet Development
Guidelines for Java Development2.1.5 Planning Servlet Development2.1.5 Planning Servlet DevelopmentThinking Ahead
Defining the Problem
Planning the Solution
Coding the Servlet
Testing and EvaluatingThinking aheadThinking aheadJ2SE SDK installation
J2SE
环境变量path和classpath是否设置正确
iCarnegie Servlet Workbench installation
Workbench安装目录下lib目录下的javax.servlet.jar是否添加到classpath中
Defining the ProblemDefining the Problem定义网页中所需控件的类型、数目以及名字(name)
定义servlet的名称,如何在网页中被调用
定义servlet处理信息得到的结果
download TravelRequest.zip to the directory where you plan to work
TravelRequest.java is a skeleton Java source file (.java) for the TravelRequest servlet.
See Listing 1 TravelRequest Template File, complete it.nullnullPlanning the SolutionPlanning the SolutionIndicate the content type being returned by the response (定义响应返回的页面的内容类型)
Retrieve an output stream to send data to the client (获取一个输出流,使用这个输出流将数据传送给客户 )
Get user input from the form: name and destination (获取用户在表单中的输入 )
Start by building the Web page header(建立网页的header)
Add the image (加入图片 )
Add the confirmation message, with the name (加入文本信息,并将从请求那里得到的姓名放入到信息中 )
End by building the Web page footer (结束网页 )nullQuestions 1? Questions 1? The servlet needs to get only two of the three user inputs from the form: the name and the destination.
How to obtain these inputs in our servlet.
Send the getParameter message to the HttpServletRequest object to get the value of the text input control Name.
Send the getParameter message to the HttpServletRequest object to obtain the value of the radio button input control Destination from the form that actually provides the name of the image file for the destination city selected. Questions 2?Questions 2?How the servlet would generate the Web page based on the user inputs obtained from the form.
To build a Web page, the servlet must output all the HTML code for the Web page.
HTML code needs an IMG tag
The HTML code must include a line of text
out.println statement should be used.
string concatenation Steps Steps Write the code to build the Web page with a header, a footer, and a confirmation message built with some dummy name.
Write the code to obtain the user's choice of destination city and to construct a Web page with the image for the destination city.
Write the code to obtain the user's name and substitute that for the dummy name in the confirmation message. nullFor each step, we should
Edit the servlet source file TravelRequest.java to include the code for that step.
Compile the servlet source file using the javac command
Run the servlet using the form in TravelRequestForm.html and the iCarnegie Servlet Workbench to see result.Coding a ServletCoding a ServletExample
Step1:
output the confirmation message including the dummy name Visitor
Compile and Run
Step2:
The getParameter statement for obtaining the value of the radio button input control called Destination
The out.println statements that output HTML code from the servlet. including the HTML code for the IMG tag whose src and alt attributes both specify the image file obtained earlier.
Compile and Run
Step3:
Add the getParameter statement for obtaining the value of the text input control called Name
Edit the out.println statement that outputs the greeting in the confirmation message to include the value obtained above instead of the dummy name Visitor.
Compile and RunTesting and Evaluating the ServletTesting and Evaluating the ServletWe do need to check that the response generated by our servlet is indeed as specified to us.
We also need to make sure that the Web page constructed by the servlet is valid HTML. (WDG)
Remember to follow the steps
Define the problem you are attempting to solve.
Make a plan on how you can do this.
Code the solution as per your plan above.
Evaluate your solution against the specification given to you.2.1 Programming with Java2.1 Programming with JavaProgramming with Objects
Java Program Development
First Look at Java
Element of a Java Servlet
Planning Servlet Development
Guidelines for Java Development2.1.6 Guidelines for Java Development2.1.6 Guidelines for Java DevelopmentThe process of Programming
The Java API Documentation
Coding Style
Commenting Source Code
JavadocDeveloping Web pages Developing Web pages Defining Web page content
定义网页内容
Planning the look of the page and the needed links
规划网页的布局和所需的链接
Implementing the Web page by writing it incrementally in small steps
一步步实现网页内容
Evaluating the Web page in two ways
评估网页
使用浏览器看网页的效果以测试HTML是否正确
是否满足规范The process of Programming (编程过程)The process of Programming (编程过程)Definition Phase
Define and/or redefine the problem
定义和/或重定义问题
Planning Phase
Plan a solution to the problem
计划
项目进度计划表范例计划下载计划下载计划下载课程教学计划下载
实现问题的解决方案
Coding Phase
Code the solution
编码实现解决方案
Evaluation Phase
Evaluate and test everything
评估和测试null define
/redefine codeplanevaluate/testNotes on ProcessNotes on ProcessProcess becomes significant as the complexity of programs increases.
Most of the time spent in evaluation and testing
Evaluate your restatement to ensure correct problem being solved
Use divide-and-conquer to solve a big problem
Code the small piece, inspect, and validate before moving to next piece.
More time spent on process implies less time spent programming.Programming is not coding. Coding is not programming. Java API DocumentationJava API DocumentationOne or more of API s need to be used for Java
API provides a set of classes and methods to a programmer, thus providing a re-usable foundation to build upon.
Java has its own Java API Documentation site that is useful for finding accurate information fast.
For example, classes and methods used in the Welcome servlet can be looked up at the Java Servlet Specification site.nullCoding StyleCoding StyleCode should be written so as to be readable
By the author at a later time
Others expected to work on it for extending functionality or debugging
Code should be efficient, to function as expected in the most optimal way in terms of time and usage of system resources.
This comes with experience and thorough knowledge of the language.
Java Coding Conventions document presents an exhaustive set of rules and guidelines for writing Java Code.Commenting Source CodeCommenting Source CodeComments an essential element of documenting code
Well-commented code is more maintainable for other developers, and reduces Software’s cost of ownership
Well-written comments should explain the purpose of the class near the beginning of the class
Each method should have at least one comment immediately before the method name to explain its purpose
Important to express intent behind code than just restating.JavadocJavadocThe Java SDK offers a tool for documenting code called Javadoc .
If you type your comments in a specified format, you can run the Javadoc program on your class and an HTML document will automatically be generated that contains your comments in Java's standard documentation format.Summary
2.1Programming with JavaSummary
2.1Programming with JavaProgramming with Objects
Java Program Development
First Look at Java
Element of a Java Servlet
Planning Servlet Development
Guidelines for Java Development