当您输入用户名时出现问题并且密码未移至页面" welcome.jsp"显示空白页面Beys,无论是正确还是不正确
的index.jsp
<form action="LoginServlet" method="post">
<fieldset style="width: 300px">
<legend> Login to App </legend>
<table>
<tr>
<td>User ID</td>
<td><input type="text" name="username" required="required" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="userpass" required="required" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</fieldset>
</form>
类LoginDao
public class LoginDao {
public static boolean validate(String name, String pass) {
boolean status = false;
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost/iqraa";
String USER = "root";
String PASS = "";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pst = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(DB_URL, USER, PASS);
pst=conn.prepareStatement("select * from user where UserName=? AND UserPassword=?");
pst.setString(1, name);
pst.setString(2, pass);
rs = pst.executeQuery();
status = rs.next();
} catch (Exception e) {
System.err.println(""+e);
}
return status;
}
}
LoginServlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
String pass= request.getParameter("userpass");
HttpSession session = request.getSession(false);
if (session != null) {
session.setAttribute("name", name);
}
if(LoginDao.validate(name,pass)){
RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
}else{
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
out.close();
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
问题在于使用请求调度程序打开输出流。
javadoc说:
应该在响应提交之前调用forward 客户端(在刷新响应正文输出之前)。如果回应 已经提交,这个方法抛出了 IllegalStateException异常。响应缓冲区中未提交的输出是 在前锋之前自动清除。
试试这个:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
// response.setContentType("text/html");
// PrintWriter out = response.getWriter();
String name = request.getParameter("username");
String pass= request.getParameter("userpass");
HttpSession session = request.getSession(false);
if (session != null) {
session.setAttribute("name", name);
}
if(LoginDao.validate(name,pass)){
RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
}else{
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
// out.close();
}