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

Thursday, 14 March 2013

Example for forward and sendRedirect based on real world

Leave a Comment

Consider the real world scenario, the milk man comes and asks for monthly payment to you in your house. Here house is the container and you are a resource existing in the container. Milk man is the client or browser.
He asks for the monthly payment to you, this is the request made by the browser to resource A. If you go inside your house and ask your mother (another resource B inside the same container) for the cash and come back and deliver to milkman this is called forward.
If you ask the milkman to speak himself to your mother inside your house or you ask the milkman to speak to your father who is in his office (different domain) then this is called redirect.
Read More...

Uploading File to the server Servlet

Leave a Comment
Uploading a file to the server

1. Create a view file form.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>Insert title here</title>
</head>
<body>
<form action="go" method="post" enctype="multipart/form-data">
Select File:<input type="file" name="fname"/><br/>
<input type="image" src="MainUpload.png"/>
</form>
</body>
</html>


Insert title here
Select File:

2. For Uploading File we are using Oreilly's MultipartRequest Class  that comes with cos.jar

UploadServlet.java
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;

public class UploadServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
		
MultipartRequest m=new MultipartRequest(request,"d:/new");

out.print("successfully uploaded");
}
}

3.web.xml (deployment descriptor)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>UploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/go</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 
Read More...

Thursday, 13 December 2012

Servlet Container

Leave a Comment
A servlet container is nothing but a compiled, executable program. The main function of the container is to load, initialize and execute servlets. The servlet container is the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications are developed by Sun under the Java Community Process.

A container handles large number of requests as it can hold many active servlets, listeners etc. It is interesting to note here that the container and the objects in a container are multithreaded. So each object must be thread safe in a container as the multiple requests are being handled by the container due to the entrance of more than one thread to an object at a time.

Note : A Servlet container may run stand alone i.e. without a web server or even on another host.

We can categorize the servlet containers as:
I. A simple servlet container is not fully functional and therefore it can only run very simple servlets and does the following :
  • Wait for HTTP request.
  • Construct a ServletRequest object and a ServletResponse object.
  • If the request is for a static resource, invoke the process method of the StaticResourceProcessor instance, passing the ServletRequest and ServletResponse objects.
  • If the request is for a servlet, load the servlet class and invoke its service method, passing the ServletRequest and ServletResponse objects. Note that in this servlet container, the servlet class is loaded every time the servlet is requested.
II. A fully functional servlet container additionally does the following for each HTTP request for a servlet:
  • When the servlet is called for the first time, load the servlet class and call its init method (once only).
  • For each request, construct an instance of javax.servlet.ServletRequest and an instance of javax.servlet.ServletResponse.
  • Invoke the servlet's service method, passing the ServletRequest and ServletResponse objects.
  • When the servlet class is shut down, call the servlet's destroy method and unload the servlet class.
Now lets see what a servlet container does for each HTTP request for a servlet, in general :
  • The servlet container loads the servlet class and calls the init method of the servlet as soon as the servlet is called for the first time.
  • Then this container makes an instance of javax.servlet.ServletRequest and javax.servlet.ServletResponse for each request.
  • Then it passes the ServletRequest and ServletResponse objects by invoking the servlet's service method.
  • Finally, it calls the destroy method and unload the servlet class when the servlet class is to be shut down.

Read More...