Servlet未转发到jsp,但正在转发到另一个servlet(请求分派器)

问题描述 投票:0回答:1

我正在尝试将请求和响应转发到jsp文件。首先,我将请求从名为Login的servlet转发到名为Home的servlet,然后在Home servlet中,将请求转发到名为home.jsp的jsp文件。 。

我所有的jsp文件都在WEB-INF文件夹中。

这是我的Login.java(Servlet):

package com.webauth.login;

import com.webauth.constants.ResponseConstants;
import com.webauth.db.DB;
import com.webauth.model.UserData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.simple.JSONObject;

/**
 *
 * @author admin
 */
public class Login extends HttpServlet {





    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

       String userID  = request.getParameter("userID");
       DB db = new DB();
       Connection connection = null;
       boolean result  = false;
       String message = "Sorry User id doesn't exist or something went wrong";
       UserData user = new UserData();
        try {
            connection = db.getConnection();
             ResultSet rs = db.checkIfUserExist(connection, userID);

             if(rs!=null){
                 if(rs.isBeforeFirst()){
                     while(rs.next()){
                         String username = rs.getString("username");
                         String email = rs.getString("email");

                         user.setUsername(username);
                         user.setEmail(email);

                         HttpSession session  = request.getSession();
                         session.setAttribute("UserData", user);

                         result  = true;

                     }
                 }

             }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
           try {
               connection.close();
           } catch (SQLException ex) {
               Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
           }
        }

        if(result){
            //This request dispatcher is working fine
            RequestDispatcher rd =  request.getRequestDispatcher("Home");
            rd.forward(request, response);
        }else{
              JSONObject res = new JSONObject();
              res.put(ResponseConstants.RESULT,result);
              res.put(ResponseConstants.MESSAGE, message);

              PrintWriter out = response.getWriter();
              out.println(res);
        }



    }



}

这是我的Home.java(Servlet):

package com.webauth.home;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Home extends HttpServlet {




    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //This is line printed onto console
            System.out.println("This line is being printed);

            //This request dispatcher is not working
           RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/home.jsp");
           rd.forward(request, response);

    }

}

这是我的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>Register</servlet-name>
        <servlet-class>com.webauth.registiration.Register</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>com.webauth.login.Login</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>Home</servlet-name>
        <servlet-class>com.webauth.home.Home</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Register</servlet-name>
        <url-pattern>/Register</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Home</servlet-name>
        <url-pattern>/Home</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

注意:我所有的jsp文件都在WEB-INF文件夹中。

我要去哪里错了?

java jsp servlets
1个回答
0
投票

尝试使用response.sendRedirect()方法将响应转发到.jsp文件。 Servlet中的forward() and sendRedirect()之间有区别。

© www.soinside.com 2019 - 2024. All rights reserved.