Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts

JSP and Servlet Interaction in Java with an example

Below example shows how to print the data that is retrieved from a Form using a JSP and then passing it to a Servlet for printing it on the GUI.
What components are needed for this ?
  • JSP Page with a form input
  • A Controller 
  • Web Configuration file (web.xml)
JSP Page :

Below is the code for the JSP Page with a Form input. The Form has the below input fields.
  • First name
  • Last name
  • City
The JSP Page code looks like below.

Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
 <form action="/Index" method="post">
        <input type="text" name="fname"/><br>        
        <input type="text" name="lname"/>
        <input type="text" name="city"/>                                                                                                                                                                         
        <input type="submit" value="submit">            
    </form>

</body>
</html>

Once we have the JSP with the Form data, we need to create the Controller. Below is the code for the controller.


ControllerServlet.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ControlServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fname = request.getParameter("fname");
        String lname = request.getParameter("lname");
        String city = request.getParameter("city");
        System.out.println("FirstName :"+ fname);
        System.out.println("Lastname :"+ lname);
        System.out.println("City :"+ city);
    }

}
Once we have created the Controller, we need to map this in the web.xml file. Below is the code for the web.xml file for the servlet mapping.

web.xml
  <servlet>
    <servlet-name>controllerServlet</servlet-name>
    <servlet-class>com.developersareana.ControllerServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>controllerServlet</servlet-name>
    <url-pattern>/Index</url-pattern>
  </servlet-mapping> 
Hope this example helps you understand how you can pass the data from the JSP to a Servlet.

How to Pass data from a JSP page to a Servlet using Plain Java J2EE

Below example explains how to use the JSP to get the Form data input and pass it to the servlet to store/update the database.


Step 1:  Create a class that extends the HttpServlet() as shown below.
public class FormServlet extends HttpServlet {}
Step 2 : 

Map this servlet in the web.xml as shown below
<servlet>
    <servlet-name>formServlet</servlet-name>
    <servlet-class>com.developersarena.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>formServlet</servlet-name>
    <url-pattern>/form</url-pattern>
</servlet-mapping>
Step 3 :  In the Form action POST method , point the action to the Form as shown below.
<form action="form" method="post">
Step 4 :
Then create a doPost() method in your servlet which takes the input as request parameters. The input field name is the request parameter name.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String fname = request.getParameter("firstname");
    String lname = request.getParameter("lastname");
  }
Step 5 : 

Pass these details to the persistence API's as show below..
User user = new User(fname, lname);
userDAO.save(user);

Difference between JSP and Servlets

interview questions for JSP and Servlets only. Since JSP and Servlets are almost identical technology, there is only one section for both JSP and Servlet interview questions. If you need interview questions for any other java related technologies , please check the relevant sections. 
Difference between JSP and Servlets
JSP is used mainly for presentation only. A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP. But a servlet can support any protocol like HTTP, FTP, SMTP etc.

Login Authentication using JSP, Servlet, MySQL in JAVA

In this example we will show you how to authenticate and login user against database username and password. This program consists of a JSP page and a Servlet to authenticate the user against database username and password.
User enters the username and password on the JSP page and clicks on the "Sign-In" button. On the form submit event, data is posted to the servlet for authenticating the user. Servlet makes JDBC connection and authenticate the user. Before submitting data to servlet, javascript is ensuring that none of the fields must be empty.
We are using tomcat server for running Servlet and JSP page. You can use any browser to test the application. We are using two files AuthenticLogin.jsp and LoginAuthentication.java and we are making the application in "webapps/JSPMultipleForms" in tomcat server.
Download Source Code