I'm building small application, im kinda newbie in jsp..I use the MVC n servlets concepts..
My problem is after from servlets, the page i want to go (change to another place) cannot shown in the address bar correctly(the addressbar still showing the servlet page), but the view is exactly d same with the page i want to go..

I create a login page in file main.jsp, i send the data to ControlIndex servlet, n if login success go to mgmtActivity.jsp, all the error things while login i saved in the list errors.

This is my servlet code:

public class ControlIndex extends HttpServlet {
    private List errors = new Vector();
    private HttpSession session;
    
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.close();
    }

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

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        session = request.getSession();
        errors.clear();                

        boolean statusLogin = false;
        String aksi = request.getParameter("aksi");
        
        if (aksi.equals("login")){        
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            User user = null;
            // input validation
            if ((username == null) || !username.trim().matches("^[a-zA-Z]+$")){
                errors.add("Username cannot contains white space or non alphabetic characters.");
            } 
            // execute
            if (errors.size()==0){
                user = User.getUser(username);
                if (user == null){
                    errors.add("Cannot find those username.");
                } else{ 
                    if (user.validatePassword(password)){
                        statusLogin = true;
                    } else{
                        errors.add("Wrong password.");
                    }                
                }
            }
            if (statusLogin){
                //set session
                session.setAttribute("user", user);
                RequestDispatcher view = request.getRequestDispatcher("mgmtActivity.jsp");
                try{
                    view.forward(request, response);
                }catch(ServletException e){
                    e.printStackTrace();
                }  
            }
        }else if (aksi.equals("logout")){
            //delete session
            session.setAttribute("user", null);
        }
        if (errors.size()!=0){
            request.setAttribute("errors", errors);
        }

        RequestDispatcher view = request.getRequestDispatcher("main.jsp");
        try{
            view.forward(request, response);
        }catch(ServletException e){
            e.printStackTrace();
        }
    }
}

Can anyone help me with this problem? Thanks before..:)

Dani AI

Generated

Server-side forwards keep the browser URL unchanged. In 's flow (main.jsp -> ControlIndex servlet -> mgmtActivity.jsp) the code uses a server-side forward, so the response body becomes mgmtActivity.jsp while the address bar still shows the servlet path. That is expected behavior. To update the browser address bar a client-side redirect must be issued instead.

A common fix (and the recommended Post/Redirect/Get pattern after processing a POST) is to send a redirect. Example:

session.setAttribute("user", user);
response.sendRedirect(request.getContextPath() + "/mgmtActivity.jsp");
return;

Notes: sendRedirect issues a 302 and changes the URL; session attributes remain, but request attributes do not. The leading context path avoids wrong relative paths.

Other practical issues visible in the posted code:

  • Avoid per-request state as servlet fields. private List errors and private HttpSession session are unsafe because servlet instances are shared across threads. Use local variables inside doPost, e.g. List<String> errors = new ArrayList<>(); HttpSession session = request.getSession();.
  • After calling forward or sendRedirect, return from the method to prevent fall-through (the posted code always forwards to main.jsp at the end — that can cause confusing behavior or IllegalStateException).
  • Prefer absolute dispatcher paths (leading slash): request.getRequestDispatcher("/mgmtActivity.jsp") so the container resolves it from the context root.
  • Guard the action parameter to avoid NPEs: "login".equals(aksi) or check for null.

On 's frames suggestion: frames or AJAX could cause similar address-bar behavior, but nothing in the posted code shows frames, so server-side forwarding is the likeliest cause. Browser dev tools (Network tab) will quickly show whether a 302 redirect or an internal forward occurred.

i think problem is with jsp...
u might be using frames...thatswhy..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.