post发送方式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="request-receiver.jsp" method="post">
username:<input type="text" name="user"/><br/>
password:<input type="password" name="pwd"/><br/>
<input type="submit" name="登陆"/>
</form>
</body>
</html>
获取数据
<%
//获取请求数据
//设置请求方式的编码
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("user");
String pwd = request.getParameter("pwd");
System.out.println(name+","+pwd);
if("smyh".equals(name) && "007".equals(pwd)) {
out.println("登陆成功");
}else{
out.println("errors");
}
%>
页面转发:
request.getRequestDispatcher("request-success.jsp").forward(request, response);
页面跳转
response.sendRedirect("页面");
重定向和转发:
重定向的意思是说,当页面进行跳转之后,request和response的生命周期已经结束,完全交给下一个页面去处理了(连url都变了);而转发的时候,还可以往request里面添加携带参数(url不变)。
//设置请求方式的编码
request.setCharacterEncoding("UTF-8");
request中可以存数据,session中也可以存数据。如果是保存用户名信息等,可以存到session中;如果只需要传递数据(从这个页面传到另一个页面),就可以放在request中转发出去,避免内存浪费。