DorisLv
2016-04-11 03:46
不是dispatcher是可以携带request去下一个页面的吗? 用户的名字和密码应该可以用request.getParameter得到的呀,可是我用了后,写出的名字是null。 请大神解答!谢谢!
request.getParameter("username").forward(requset,response)
后面还有参数 是不是这样写
<%@ 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=ISO-8859-1"> <title>LOGIN PAGE</title> </head> <body> <h1>User Login</h1> <hr> <form action="dealwithlogin.jsp" name="loginForm" method="post"> <table> <tr> <td>UserName:</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>Password:</td> <td><input type="text" name="password"/></td> </tr> <tr> <td colspan="2"><input type="submit" name="login"/></td> </tr> </table> </form> </body> </html>
这是login的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username="";
String password="";
request.setCharacterEncoding("utf-8");
username=request.getParameter("username");
password=request.getParameter("password");
if("admin".equals(username)&&"12345".equals(password)){
request.getRequestDispatcher("login_success.jsp").forward(request,response);
}
else{
response.sendRedirect("login_failure.jsp");
}
%>这是dealwithlogin的代码
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>login success</h1><br>
Welcome: <%=request.getAttribute("username") %><br>
<%
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String s=sdf.format(date);
out.println(s+"<br>"); %>
</body>
</html>这是login success的代码
这是我执行后的截图,
JAVA遇见HTML——JSP篇
248273 学习 · 3215 问题
相似问题