简体   繁体   中英

RequestDispatcher not working from servlet doPost to other servlet doGet

I'm trying to forward from one servlet's doPost method to another servlet's doGet method. However when I'm running the webapp, the URL doesn't change. When I enter the URL of the second servlet manually, the servlet works perfectly. When I enter a webpage in the RequestDispatcher it also works.

The first servlet is a loginservlet. The doPost method of this servlet handles the database lookup for the user. Next, based on the type of user (artist, listener or admin), the servlet redirects to their corresponding homepage via a second servlet's doGet method. This last step is required and cannot be skipped as I'll write more code in this method.

Here's the login servlet:

public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 req.getRequestDispatcher("/login.jsp").forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    if(request.getParameter("username") != null && request.getParameter("password") != null &&
            request.getParameter("username") != "" && request.getParameter("password") != ""){
    //Check if username exists
    UserHelper helper = new UserHelper();


    if(helper.userExists(request.getParameter("username")) == 1){
        User user = helper.getUserByUsername(request.getParameter("username"));
        if(user.getUserPassword().equals(helper.hashPassword(request.getParameter("password")))){
            HttpSession session = request.getSession();
            session.setAttribute("user", request.getParameter("username"));

            if(user instanceof Artist){

            Artist artist = new Artist();
            artist = (Artist) user;
            session.setAttribute("Artist", artist);
            System.out.println("OK");
            request.getRequestDispatcher("/artist/artistpanel").forward(request, response);
            }
            if(user instanceof Listener){
            Listener listener = new Listener();
            listener = (Listener) user;
            session.setAttribute("Listener", listener);
            request.getRequestDispatcher("/listener/store.jsp").forward(request, response);
            }
            if(user instanceof Admin){
            Admin admin = new Admin();
            admin = (Admin) admin;
            session.setAttribute("Admin", admin);

            }

            /*response.sendRedirect("artist/artisthome.jsp?success=login");*/
        }else{
            request.setAttribute("error", "U gaf een foutief passwoord op. Probeer nogmaals."); 
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }else{
        request.setAttribute("error", "Er bestaat geen gebruiker met die username."); 
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }
}else{
    request.setAttribute("error", "Beide velden moeten worden ingevuld."); 
    request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}

Here's the ControlPanel servlet code:

package Controller.Servlets;

import Model.Artist;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author Daan
*/
public class ArtistPanelServlet extends HttpServlet {

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

    Artist artist = new Artist();

    HttpSession session = request.getSession();
    artist = (Artist) session.getAttribute("Artist");

    session.setAttribute("Artist", artist);
    request.setAttribute("ArtistName", artist.getArtistName());

    request.getRequestDispatcher("/artist/artisthome.jsp").forward(request, response);
}


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


@Override
public String getServletInfo() {
    return "Short description";
}
}

Here a part of my servlet mapping.

<servlet>
    <servlet-name>ArtistPanelServlet</servlet-name>
    <servlet-class>Controller.Servlets.ArtistPanelServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ArtistPanelServlet</servlet-name>
    <url-pattern>/artist/artistpanel</url-pattern>
</servlet-mapping>

Thanks for you help.

The only way to redirect it to doGet is to have your doPost call doGet, which makes them both do the same thing:

public void doPost(HttpServletRequest request, HttpServletResponse  response) throws IOException, ServletException
{
    doGet(request, response);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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