Servlet send response to JSP

<html>
<head>
</head>
<body>
<form name="loginform" method="post" action="WelcomeServlet">
<br><br>
<table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table>
<table width="300px" align="center" style="border:1px solid #000000;background-color:#efefef;">
<tr><td colspan=2></td></tr>
<tr><td colspan=2>&nbsp;</td></tr>
    <tr>
        <td><b>Login Name</b></td>
        <td><input type="text" name="username" ></td>
    </tr>
    <tr>
        <td><b>Password</b></td>
        <td><input type="password" name="password"></td>
    </tr>

    <tr>
        <td></td>
        <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    <tr><td colspan=2>&nbsp;</td></tr>
</table>
</form>


</body>
</html>

then this servlet

import java.io.*;
import java.util.*;
//import java.io.PrintWriter;
 import javax.servlet.*;
//import javax.servlet.ServletConfig;
//import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WelcomeServlet extends HttpServlet {
/* 
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
 */

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Get the value of form parameter
*/
response.setContentType("text/html");

PrintWriter out = response.getWriter();
//out.println("I am on welcome servlet...");
String username = request.getParameter("username");
String password =request.getParameter("password");

out.println("<html>");
         out.println("<head>");
         out.println("<title> A very simple servlet example</title>");
         out.println("</head>");
          out.println("<body>");
            out.println("</body>");

      if((username.equals("kiran"))&&(password.equals("kiran")))
       {

         String welcomeMessage = "Welcome "+username+" thanks for login...";
         out.println("<h1>"+welcomeMessage+"</h1>");
      request.getRequestDispatcher("/login.jsp").include(request, response);

    }else
      {
         out.println("<h1> You are not the valid user...</h1>");
    request.getRequestDispatcher("/login.jsp").include(request, response);

        }

            out.println("</html>");
            out.close();





}

public void destroy() {

}
} 

I want a response from servet which is display below the jsp page login authetication table


That's not entirely right. In contrary to what lot of basic servlet tutorials try to let you believe, the servlet should not be used to output pure HTML. This contradicts the MVC ideology. There the JSP should be used for.

In this particular case, you need to let the servlet set the message which you'd like to display in the JSP in the request scope and then forward the request/response to the JSP. In the JSP, you can use JSTL to dynamically control the HTML output and use EL ${} to access and display the message.

Here's a kickoff example of how the servlet should look like:

public class WelcomeServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String message = null;

        if ((username.equals("kiran")) && (password.equals("kiran"))) {
            message = "Welcome "+username+" thanks for login...";
        } else {
            message = "You are not the valid user...";
        }
    
        request.setAttribute("message", message);
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }

} 

and edit your login.jsp to add the following:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

...

<c:if test="${not empty message}">
    <h1>${message}</h1>
</c:if>

The taglib declaration has to go in the top. The <c:if> can just be positioned exactly there where you'd like to display the <h1>.

See also:

  • Our Servlets wiki page - Contains a Hello world which covers basic validation.