-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.java
More file actions
128 lines (114 loc) · 4.03 KB
/
hello.java
File metadata and controls
128 lines (114 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class hello extends HttpServlet {
/**
* Constructor of the object.
*/
public hello() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
/*
* 以Get方式访问页面时执行该函数 执行doGet前会先执行getLastModified,如果浏览器发现getLastModified返回数值
* 与上次访问返回数值相同,则认为该文档没有更新,浏览器执行缓存而不执行doGet 如果返回-1则认为是实时更新的,总是执行该函数
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.log("执行 doGet 方法...");
this.execute(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred 执行前不会执行getLastModified
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.log("执行 doPost 方法...");
this.execute(request, response);
}
/**
* 返回该Servlet生成文档的更新时间。对Get方法有效 返回的时间为相对于1970年1月1日08:00:00的毫秒数
* 如果返回-1表示实时更新。默认为-1
*/
@Override
public long getLastModified(HttpServletRequest request) {
this.log("执行 getLastModified 方法...");
return -1;
}
// 执行方法
private void execute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");// 设置request和response编码,两个都要注意
request.setCharacterEncoding("UTF-8");
String requestURI = request.getRequestURI();// 访问Servlet的URI
String method = request.getMethod();// 访问Servlet的方式Get或Post
String param = request.getParameter("param");// 客户端提交的参数param值
response.setContentType("text/html");// 设置文档类型为HTML类型
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
out.println("<HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" 以" + method + " 方式访问该页面。提取的param参数为:" + param + "<br/>");
// input
out.println(" <form action='" + requestURI + "' method='get'>"
+ "<input type='text' name='param' value='param string'>"
+ "<input type='submit' value='以Get方式查询页面 '" + requestURI
+ "'>" + "</form>");
out.println(" <form action='" + requestURI + "' method='post'>"
+ "<input type='text' name='param' value='param string'>"
+ "<input type='submit' value='以Post方式查询页面 '" + requestURI
+ "'>" + "</form>");
// 由客户端浏览器读取该文档的更新时间
out.println(" <script>document.write('本页面的最后更新时间: '+document.lastModified);</script>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}