Showing posts with label J2EE Tutorials. Show all posts
Showing posts with label J2EE Tutorials. 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);

What are the Applet's Life Cycle methods? Explain them?

* init() method - called when an applet is first loaded. This method is called only once in the entire cycle of an applet. This method usually intialize the variables to be used in the applet.
* start( ) method - called each time an applet is started.
* paint() method - called when the applet is minimized or refreshed. This method is used for drawing different strings, figures, and images on the applet window.
* stop( ) method - called when the browser moves off the applet's page.
* destroy( ) method - called when the browser is finished with the applet.

Java Architecture Diagram

J2EE Tutorial-An Introduction


The Java language is such that it allows cross-platform communication between multiple kinds of devices. For example, a programmer can develop Java code on a desktop computer and expect it to run on other computers, routers, and even mobile phones, as long as those devices are Java-enabled. This portability is described by the Sun acronym WORA, which stands for "Write once, run anywhere." A large number of mainframes, computers, mobile phones, and other electronic devices operate using the Java Platform.
The 2 in the acronym J2EE stands for Version 2. As with many software applications, J2EE is Java Platform Version 2. Actually, the number 2 is often dropped nowadays, so J2EE becomes Java EE. Traditionally, though, it's still J2EE.
Now, on to the EE. It stands for Enterprise Edition, which is a powerful form of the Java Platform. Sun has created three editions so far. The most precise is the Micro Edition, which is used for mobile phones and PDAs. Following form, this can be abbreviated as Java ME.
The middle edition is the Standard Edition, which can run on mobile devices, laptops and desktop computers. The abbreviated name of this edition is Java SE. Building our way up the pyramid, we come at last to the Enterprise Edition, which includes all the functionality of the Micro Edition and the Standard Edition and also features routines and subroutines designed specifically for servers and mainframes.