JSP中request與response

一、獲取表單提交的數據html

<%@ 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>
<%@ 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>

二、get與post的區別java

三、中文亂碼jsp

四、在請求中存取屬性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 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>
<%@ 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>
<%@ 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>

五、轉發與重定向ui

相關文章
相關標籤/搜索