1, Request
action: a package HTTP request message.
Method:
. 1) the getParameter (String): Get Parameter
2) getParameterValues (String): Get Parameter Value
3) getParameterNames (): Get a list of parameter name
Example:
login.jsp:
<form action="C" method="post">
姓名:<input type="text" name="name"><br>
爱好:<input type="checkbox" name="hobby" value="read">读书
<input type="checkbox" name="hobby" value="football">足球
<input type="checkbox" name="hobby" value="run">跑步 <br>
性别:<input type="radio" name="sex" value="male">男
<input type="radio" name="sex" value="female">女<br>
<input type="submit" value="提交">
</form>
CServlet:
// 获取名字<input type="text" name="name">的值
String name = request.getParameter("name");
// 爱好<input type="checkbox" name="hobby" value="read">
String[] hobbys = request.getParameterValues("hobby");
// 性别<input type="radio" name="sex" value="male">
String sex = request.getParameter("sex");
// 显示到页面上
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("你叫:" + name + "<br/>");
out.print("你的爱好是:<br/>");
for (String h : hobbys) {
out.print(h + "<br/>");
}
out.print("你的性别是:" + sex + "<hr/>");
Note: hash issues
i) If the request is submitted by the get method (either post or can get)
String name = request.getParameter ( "name");
// Get the specified bytecode format
byte [] bt = name.getBytes ( "ISO-8859-1");
//. 8 re-encoded according to UTF
String NAME1 = new new String (BT, "UTF-. 8");
II) the request is valid only for the post
request.setCharacterEncoding ( "utf-8") ;
String name = request.getParameter ( "name");
Note: be sure to call getParameter once in the first () set the encoding before.
4) getRequestDispatcher (): returns a RequestDispatcher object.
5) setAttribute () | getAttribute (
concept: the request is forwarded to another component inside the server to perform again, while sharing the data request.
Features: Client submitted only one request. Browser url does not change. Forwarding resources occur in the same web server project.
example:
FirstServlet:
System.out.println("我是黄牛!");
// 借助request可以进行数据共享
request.setAttribute("money", 15);
// 请求转发
RequestDispatcher rdDispatcher = request.getRequestDispatcher("/Second");
rdDispatcher.forward(request, response);
SecondServlet:
System.out.println("我是黄牛的亲戚!");
// 可以获取request域中的数据
int money = (int)request.getAttribute("money");
System.out.println("收到:"+money+"块钱!");
request.removeAttribute("money");
// 响应
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("车票!");
Note: request a domain object, (a request until the end of the second request) can only be sharing data with a request that can be used only when forwarding.
2, Response
function: mainly transmits a response message, the redirection.
Method:
. 1) the getWriter () Write ():. Sends a response message to the client.
2) sendRedirect (): Redirect
concept: Redirect is to send the request back to the browser. Then automatically re-sends a request to the server by the browser.
Features: the user sends two requests. Browser url change. Redirection can occur between different servers.
Example. 1:
Response.sendRedirect ( "http://news.baidu.com");
Note: redirect url must be a server path, or relative to the current server.
Example 2:
Response.sendRedirect (GetServletContext () getContextPath () + "/ F..");
The difference between the forwarding and redirection of?
. 3, the Session
Session: open the browser from the start, comprising multiple requests and responses to close the browser until.
Note: Session will create different for different client browser. A common mistake is to think that session is created when there is client access, but the fact is that until a server-side programs (such as Servlet) call HttpServletRequest.getSession (true) or HttpServletRequest.getSession () will only be created when such a statement .
example:
AServlet:
HttpSession hSession = request.getSession();
hSession.setAttribute("name", name);
response.getWriter().write("welcome:"+hSession.getAttribute("name"));
BServlet:
HttpSession hSession = request.getSession();
response.getWriter().write("I am R, welcome:"+hSession.getAttribute("name"));
注意:程序调用HttpSession.invalidate()时销毁session对象,或者超过30分钟(可以自己设定)。