JSP中request与response

1、获取表单提交的数据

151801_gXAs_2320342.jpeg

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ 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 id="" action="doCreateUser.jsp" method="get">
  用户名:<input type="text" name="userName"/>
  <input type="submit" value="提交"/>
 </form>
</body>
</html>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ 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>
 <%
  String userName = request.getParameter("userName");
  out.print(userName);
 %>
</body>
</html>

2、get与post的区别

154228_BSmd_2320342.png

154522_HFBg_2320342.png

3、中文乱码

155215_jk61_2320342.jpeg

4、在请求中存取属性

162727_bAT1_2320342.jpeg

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ 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 id="" action="doCreateUser.jsp" method="post">
  用户名:<input type="text" name="userName"/>
  <input type="submit" value="提交"/>
 </form>
 <%
  // 取回提示信息
  Object oMess = request.getAttribute("mess");
  if (oMess != null) {
   out.print(oMess.toString());
  }
 %>
</body>
</html>
?
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
<%@ 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>
 <%
  request.setCharacterEncoding("UTF-8");
  String userName = request.getParameter("userName");
  //out.print(userName);
   
  if (userName.equals("admin")) {
   // 加入提示信息
   request.setAttribute("mess","注册失败,更换用户名。");
   request.getRequestDispatcher("createUser.jsp").forward(request, response);
   //response.sendRedirect("createUser.jsp");
  } else {
   request.setAttribute("mess","注册成功。");
   request.getRequestDispatcher("default.jsp").forward(request, response);
   //response.sendRedirect("default.jsp");
  }
 %>
</body>
</html>
?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
 <%=(request.getAttribute("mess")).toString() %>
</body>
</html>

5、转发与重定向

163323_3sh7_2320342.jpeg

163411_3Ws9_2320342.png

163803_3e28_2320342.png

转载于:https://my.oschina.net/u/2450896/blog/663995

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值