简体   繁体   中英

How to redirect from one JSP page to another fresh page

I am new to JSP ans servlets. I have written simple jsp code to display registration form when clicked on "SIGN UP" button as below. signup.jsp looks like

<!DOCTYPE html>
<html>
<body>
<h2>Welcome </h2>
<img src="abcd.jpg" alt=" " style="width:500px;height:228px;">  
<form name="main"  method="get" action="login.jsp">
        <input type="submit" name="ter" value="SIGN IN" >
 </form>  
</body>
</html>

When I run this signup.jsp is getting displayed with image and "SIGN IN" button. When I click on this button, login page is getting displayed on the same page where signup.jsp displayed. How can i modify the jsp code such that SIGN IN details will come in a fresh page instead on the same page. My login.jsp looks like

<%@ include file="index.jsp" %>  
<hr/>  

<h3>Login Form</h3>  
<%  
String profile_msg=(String)request.getAttribute("profile_msg");  
if(profile_msg!=null){  
out.print(profile_msg);  
}  
String login_msg=(String)request.getAttribute("login_msg");  
if(login_msg!=null){  
out.print(login_msg);  
}  
 %>  
 <br/>  
<form action="loginprocess.jsp" method="post">  
Email:<input type="text" name="email"/><br/><br/>  
Password:<input type="password" name="pass"/><br/><br/>  
<input type="submit" value="login"/>"  
</form>  

can anyone suggest me how to write it using JSP only as I am completely new to java script or other technologies also.

This is how my servlet looks like:

@WebServlet("/SignInFunc")
@MultipartConfig
public class MySignInFunc extends HttpServlet {
    /** * */
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
        rd.forward(request, response);
    } 
}

when i click on Sign in , fields mentioned in login.jsp are getting displayed on the same page. I want them on fresh page.

You'll need to create WebServlet and post login request, then using HttpServletRequest and then when you'll be sure that user information is correct forward user to necessary page request.getRequestDispatcher("index.jsp").forward(request, response); You'll allso need to add <form action = "login" method = "post"> this kind of thing to your login form and WebServlet would look like this:

@WebServlet("/login")
public class HandleLogin extends HttpServlet {

    public HandleLogin() {
        super();
    }

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

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ...
        response.sendRedirect("/home.jsp");
    }
} 

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