重定向不在jsp中工作

时间:2014-03-24 09:42:32

标签: java jsp servlets redirect

我创建了一个jsp页面view.jsp,并且对应于我创建了一个servlet admin.java 以下两个代码...... 当我点击注册时出现空白页面...重定向不起作用。请帮我解决这个问题

view.jsp的

 <%@ page language="java" 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>
    <form action="admin">
    username <input type="text" name="username" value="" />
    password <input type="text" name="password" value="" />
    <input type="submit" name="register" value="register" />"
    </form>
    </body>
    </html>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                admin.java



 import java.io.*;


    import java.sql.*;


    import javax.servlet.*;
    import javax.servlet.http.*;


    public class admin extends HttpServlet {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/inventory";
    static final String USER = "root";
    static final String PASS = "root";


    Connection conn = null;
    Statement stmt = null;

    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {



    try {

    String user = req.getParameter("username");
    String pass = req.getParameter("password");


    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = conn.createStatement();
    String sql = "select * from admins";
    //String sql = "select * from admins WHERE username='"+user+"' AND     

    password='"+pass+"'";
    ResultSet rs = stmt.executeQuery(sql);


    PrintWriter pw = res.getWriter();
    //res.setContentType("text/html");



    while (rs.next()) 
    {
    if((user.equals(rs.getString(0))) &&  (pass.equals(rs.getString(1))))
    {
    //String n=rs.getString("username");
    //String p=rs.getString("password");
    res.sendRedirect("loginsuccess.jsp");

    }
    else

    {
    res.sendRedirect("loginfailure.jsp");
    }
    }

    pw.close();

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    }

4 个答案:

答案 0 :(得分:0)

使用RequestDispatcher代替sendRedirect()

RequestDispatcher reqDispatcher = req.getRequestDispatcher("path/loginsuccess.jsp");
            reqDispatcher.forward(req, res);

详细了解RequestDispatcher

阅读RequestDispatcher and sendRedirect之间的差异,并同时使用它们。

答案 1 :(得分:0)

试试这个

getServletContext().getRequestDispatcher("/loginsuccess.jsp").forward(request, response);

答案 2 :(得分:0)

示例代码:

User user = userDAO.find(username, password);
if (user != null) {
    request.getSession().setAttribute("user", user); // Login user.
    response.sendRedirect("home"); // Redirects to http://example.com/context/home after succesful login.
} else {
    request.setAttribute("error", "Unknown login, please try again."); // Set error.
    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to same page so that you can display error.
}

你应该尝试这种方法,但我的建议是:

  1. 我认为你不应该close the statement, result set and connection,

  2. 然后你应该检查if condition。如果您的情况可能有错误,页面就像空的一样。所以应该检查..

  3. 来自:https://stackoverflow.com/a/2048640/3242978

答案 3 :(得分:0)

当您单击“注册”按钮时,它会将表单作为HTTP POST请求提交。您需要实现doPost()。