Archive

Archive for the ‘Servlet & JSP’ Category

Difference between ServletConfig and ServletContext

November 2, 2011 1 comment
S.No ServletConfig ServletContext
1. A ServletConfig object passes configuration information from the server to a servlet. ServletConfig also includes ServletContext object A ServletContext defines a set of methods that a servlet uses to communicate with its servlet container.
2. There is one ServletConfig parameter per servlet. There is only one SevletContext for the entire web application.
3. The param-value pairs for ServletConfig object are specified in the “init-param” within the “servlet” tags in the web.xml file The param-value pairs for ServletContext object are specified in the “context-param” tags in the web.xml file.
4. Init parameters for ServletConfig are defined within “servlet” element for each specific servlet

<servlet>
			<servlet-name>SampleServlet</servlet-name>
			<servlet-class>com.sample.test.SampleServlet</servlet-class>
				<init-param>
					<param-name>foo</param-name>
					<param-value>bar</param-value>
				</init-param>
		</servlet>
		
Init parameters for ServletContext are defined within the “web-app” element but NOT within a specific “servlet” element.

<web-app>
			<context-param>
				<param-name>foo</param-name>
				<param-value>bar</param-value>
			</context-param>
			<!-- Other stuff -->
		</web-app>
		
5. Servlet Code:

getServletConfig().getInitParameter("foo");
Servlet Code:

getServletContext().getInitParameter("foo");
Advertisements

Difference between GET and POST method in HTTP

October 19, 2011 Leave a comment
S.No GET POST
1. The data passed using the GET method would be visible to the user of the website in the browser address bar The data passed using POST method is not visible to the user directly
2. GET method form-data parameters are appended to the URL in name/value pairs and sent along with header information POST method sends form-data as an HTTP post transaction.
3. Parameters are not encrypted Parameters are encrypted
4. GET method has maximum length restricted to 256 characters POST method has no size limitations
5. GET method is designed for reading the information from server( a document, a chart, or a database query ) POST method is designed for posting information to the server
6. GET method is faster than POST method.Because it is requesting information from server POST method is slow compared to GET. Because it is updating information to the server
7. GET method is not secure POST method is secure
8. It allows bookmarks It disallows bookmarks

Simple Webapplication using Servlet and JSP

October 13, 2011 4 comments

Here in this post we will see simple web application using Servlet and JSP where we are going to give customer details in JSP page then get the customer details in servlet and show the customer details inserted in Welcome page.

Environment used:
1. Eclipse 3.3
2. JDK6
3. Tomcat Servler 6.x

1. Create “Dynamic Web Project” with Dynamic web module version – 2.5 Then Click Finish.

Web application structure in eclipse will look like this.

 

WebAppStructure

WebAppStructure

2. Create a Customer.jsp page inside WebContent folder of web application structure.

<%@ 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>Customer Details</title>
</head>
<body>
<form action="insertCustomer" method="post">
<table align="center" bgcolor="#99FFCC" border="1" width="70%">
	<tr>
		<td colspan="2" align="center">Customer Details </td>
	</tr>
	<tr>
		<td>Name </td>
		<td><input type="text" name="name" maxlength="25"></td>
	</tr>
	<tr>
		<td>Address </td>
		<td><input type="text" name="address" maxlength="40"></td>
	</tr>
	<tr>
		<td>Mobile </td>
		<td><input type="text" name="mobile" maxlength="10"></td>
	</tr>
	<tr>
		<td>EmailId </td>
		<td><input type="text" name="emailid" maxlength="30"></td>
	</tr>	
	<tr>
		<td colspan="2" align="center"><input type="submit" value="Submit"></td>
	</tr>	
</table>
</form>
</body>
</html>

This customer page will look like the below.

 

CustomerPage

CustomerPage

 

3. Create a InsertCustomerServlet.java servlet class. While submitting the form from the “Customer.jsp” page the servlet will get the form value in HttpServletRequest object .
 

package com.room.sample.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.room.sample.view.Customer;

public class InsertCustomerServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	public void doPost(HttpServletRequest request, HttpServletResponse response){
		System.out.println("----- InsertCustomerServlet -----");
		try {
		// Get the customer value submitted from Customer.jsp page through HttpServletRequest object
			String name=request.getParameter("name");
			String address=request.getParameter("address");
			String mobile=request.getParameter("mobile");
			String emailid=request.getParameter("emailid");
			
			//Set the Customer values into Customer Bean or POJO(Plain Old Java Object) class
			Customer customer=new Customer();
			customer.setName(name);
			customer.setAddress(address);
			customer.setMobile(Long.valueOf(mobile));
			customer.setEmailid(emailid);
			
			RequestDispatcher dispatcher=request.getRequestDispatcher("/Welcome.jsp");
			//Set the customer instance into request.Then only the customer object 
			//will be available in the Welcome.jsp page
			request.setAttribute("cust",customer);
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

}

4. Bean Class to store information received in Servlet class.

package com.room.sample.view;

import java.io.Serializable;

public class Customer implements Serializable{

	private static final long serialVersionUID = 1L;
	private String name;
	private String address;
	private Long mobile;
	private String emailid;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Long getMobile() {
		return mobile;
	}
	public void setMobile(Long mobile) {
		this.mobile = mobile;
	}
	public String getEmailid() {
		return emailid;
	}
	public void setEmailid(String emailid) {
		this.emailid = emailid;
	}
	
}

5. Create Welcome.jsp page to display the customer details processed.

<%@page import="com.room.sample.view.Customer"%>
<%@ 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>View Customer Details</title>
</head>
<body>
<%
	Customer customer=(Customer)request.getAttribute("cust");
%>
<table align="center" bgcolor="#FFFFCC" border="1" width="70%">
	<tr>
		<td colspan="2" align="center"><%="Welcome "+customer.getName()+" !!!!. Your details Processed." %></td>
	</tr>
	<tr>
		<td>Name </td>
		<td><%=customer.getName()%></td>
	</tr>
	<tr>
		<td>Address </td>
		<td><%=customer.getAddress() %></td>
	</tr>
	<tr>
		<td>Mobile </td>
		<td><%=String.valueOf(customer.getMobile()) %></td>
	</tr>
	<tr>
		<td>EmailId </td>
		<td><%=customer.getEmailid() %></td>
	</tr>	
</table>
</body>
</html>

6. Connect the flow using web.xml. For a servlet defined , the element value under element and element value under should be same.
Then only the requested action “insertCustomer” can find the corresponsing servlet to be executed from web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Simple Customer Application</display-name>
  <welcome-file-list>
    <welcome-file>Customer.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>customer</servlet-name>
  	<servlet-class>com.room.sample.servlet.InsertCustomerServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>customer</servlet-name>
  	<url-pattern>/insertCustomer</url-pattern>
  </servlet-mapping>
</web-app>

Welcome.jsp page will look like the below.

 

Welcome

Welcome