简体   繁体   English

如何从servlet重定向到jsp页面

[英]how to redirect to a jsp page from a servlet

well I have a jsp page with a login form, i'm using a servlet, if the username and password are correct the servlet redirects the user to another page else it redirects him to the login page again 好吧,我有一个带有登录表单的jsp页面,我使用的是servlet,如果用户名和密码正确,则servlet会将用户重定向到另一个页面,否则它将用户再次重定向到登录页面

when i log in with a correct username and password i'm redirected perfectly to reservation.jsp but when i put a wrong username or password in the form when i click on the submit button the page became blank 当我使用正确的用户名和密码登录时,我完美地重定向到了reservation.jsp,但是当我单击提交按钮时,在表单中输入了错误的用户名或密码时,该页面变为空白

here is the servlet 这是servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.PreparedStatement;

/**
 * Servlet implementation class LogServlet
 */
@WebServlet("/LogServlet")
public class LogServlet extends HttpServlet {
private static final long serialVersionUID = 1L;




public LogServlet() {
    super();

}



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");


    String name=request.getParameter("name");
    String password=request.getParameter("password");

    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/log",
                "root","");
        ps = (PreparedStatement) conn.prepareStatement("select nom_client,username,password from client where username = ? and password = ?");

        ps.setString(1, name);
        ps.setString(2, password);
        rs=ps.executeQuery();



    try {
        while(rs.next()){
        if(password.equals(rs.getString("password")) && name.equals(rs.getString("username"))){

        HttpSession session=request.getSession();
        session.setAttribute("name",name);
        PrintWriter out=response.getWriter();
        request.getRequestDispatcher("reservation.jsp").include(request, response);
        }
        else{
            response.sendRedirect("/login.jsp");

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

    }
    System.out.close();


    }
    catch(Exception e){e.printStackTrace();}


}
}

use request.getContextPath() , It will redirect you to login page. 使用request.getContextPath() ,它将把您重定向到登录页面。

response.sendRedirect(request.getContextPath()+"/login.jsp");

and if you are reaching to your login page in that case, pass request and response objects to your jsp page. 并且在这种情况下,如果您要访问登录页面,则将requestresponse对象传递到jsp页面。

request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request,response);

use this. 用这个。

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

As you have used 如您所用

request.getRequestDispatcher("reservation.jsp").include(request, response); 

to forward after successful login and as it is working, why not forwarding after failed also in this way .? 要在成功登录后继续转发并且正在工作,为什么也不能以这种方式转发失败后的转发?

Which mean to use 使用哪个意思

request.getRequestDispatcher("/login.jsp").include(request, response); 
request.getRequestDispatcher("login.jsp").include(request, response);

Have u tried above code in else part ? 您是否在其他部分尝试过上述代码? I think it should work 我认为应该可以

Problem is response.sendRedirect(...) does not prepend the context path where the resource (jsp) is bundled. 问题是response.sendRedirect(...)没有在资源(jsp)所在的上下文路径前添加前缀。 Try using request.getRequestDispatcher(...) in both cases. 在两种情况下都尝试使用request.getRequestDispatcher(...)

The problem could be here: 问题可能在这里:

response.sendRedirect("/login.jsp"); response.sendRedirect是( “/ login.jsp的”);

This means that in the location header of the redirect response you will have: 这意味着在重定向响应的位置标题中,您将具有:

http://localhost:8080/login.jsp HTTP://本地主机:8080 / login.jsp的

If you change it to: 如果将其更改为:

response.sendRedirect("login.jsp"); response.sendRedirect是( “login.jsp的”);

than its relative to your webabb context path: 比其相对于您的webabb上下文路径:

http://localhost:8080/app/login.jsp HTTP://本地主机:8080 /应用/ login.jsp的

Try with firebug or similar and trace down request/response you should find out quickly. 尝试使用Firebug或类似工具,并跟踪您应该迅速发现的请求/响应。 And be sure jsp files are were expected in the hierarchy. 并且确保层次结构中应包含jsp文件。

Hope this helps 希望这可以帮助

Rather than giving response.sendRedirect("\\login.jsp"); 而不是给出response.sendRedirect(“ \\ login.jsp”); use response.sendRedirect("/yourprojectname/"); 使用response.sendRedirect(“ / yourprojectname /”); and in welcome file in web.xml set login.jsp as welcome file. 并在web.xml的欢迎文件中将login.jsp设置为欢迎文件。

Add this 加上这个

response.sendRedirect("/yourprojectname/");

and set login.jsp as welcome file in web.xml 并在web.xml中将login.jsp设置为欢迎文件

来自数据库的响应为空,因此我不得不将该案例添加到类中(通过if/else条件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM