1

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.

GROX13
  • 4,605
  • 4
  • 27
  • 41
kala
  • 63
  • 2
  • 13
  • Not sure what you want ... but first, if you could use a link to go to login.jsp since you don't use the form at all. From what I can see, you use three different page (signin, login, loginprocess), so I am not sure I understand your question... – AxelH Oct 25 '16 at 06:21
  • You could take a look at this post, this is a great source to write jsp without Java. http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – AxelH Oct 25 '16 at 06:22
  • If you "inculde" the other file, it will be shown on the same page for sure. Remove first line "...include file="index.jsp" ..." – Stefan Oct 25 '16 at 06:27
  • Thanks stefan. After removing include, it worked perfectly as expected. thanks a lot. – kala Oct 25 '16 at 16:03

1 Answers1

0

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");
    }
} 
GROX13
  • 4,605
  • 4
  • 27
  • 41
  • my jsp is like
    this and my servlet has following code.
    – kala Oct 25 '16 at 15:59
  • @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 it on fresh page – kala Oct 25 '16 at 16:00
  • [kala](http://stackoverflow.com/users/5916953/kala) you can read more [here](https://www.tutorialspoint.com/servlets/servlets-page-redirect.htm) about redirection from servlets. Usually this `response.sendRedirect("/home.jsp")` kind of thing should work. – GROX13 Oct 26 '16 at 06:27