Servlet没有向jsp发送但是转发到另一个servlet(Request Dispatcher)

时间:2017-01-12 12:00:36

标签: java jsp servlets

嘿我正在尝试将请求和响应转发给jsp文件。首先,我将请求从名为登录的servlet转发到名为 Home 的servlet,然后在 Home servlet中将请求转发给jsp文件名为 home.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文件夹中。

有人可以告诉我哪里出错了。 THANKYOU

1 个答案:

答案 0 :(得分:0)

尝试使用response.sendRedirect()方法转发对.jsp文件的响应。 Servlet中forward() and sendRedirect()之间存在差异。