Skip to main content

Posts

Showing posts from July, 2013

Servlet Context Basics

Context variable can access thorough the entire application. Shared across servlets and users .But session object can't be shared across the user . so it the is an advantage of the context variable  over session. Now lets consider how context can be created and use the same across the entire application: package servlets.first; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /**  * Servlet implementation class SessionTest  */ @WebServlet(description = "Write data to session and read data from session and from context as well", urlPatterns = { "/SessionTest" }) public class SessionTest extends HttpServlet {     private static final long se

Session creation Using servlet

Using Java Servlet session variable is a mechanism to store user’s data in server. And sessions will last till we are on the site, it will be destroyed when the user quit the site. One main example of session usage is login information. When a user login to a site with his user name and password, sessions will be created for that user in server. By using these session variables servers  programs identify each user Now let's consider about how to write data in to session and read data from the session: package servlets.first; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /**  * Servlet implementation class SessionTest  */ @WebServlet(description = "Write data to session and read data from sessi

Servlet program to read POST Data

Servlet program to read POST Data in order to pass a post parameter we need a html form consider the following html page: <pre class="prettyprint" > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Post parameter </title> </head> <body> <b>Servlet Post</b> <form method="post" action="PostTest">     //Her the action redirect to the servlet path <input type="text" name="uname" /> <input type="submit" value="Enter" > </form> </body> </html> < /pre > Servlet code would be looks like this: package servlets.first; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpS

Servlet program to read get request or URL data

Servlet program to read get request or URL data Here i am not writing all the code from scratch instead i just provide you the doGet method only.      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        String uname;        response.setContentType("text/html");        PrintWriter out= response.getWriter();        uname= request.getParameter("Uname");                      out.println("<B>Uname:" +uname +"</B>");              }

A simple servlet program that print hello World

Servlet Simple code for Hello World // Import required java libraries import java . io .*; import javax . servlet .*; import javax . servlet . http .*; // Extend HttpServlet class public class HelloWorld extends HttpServlet { private String message ; public void init () throws ServletException { // Do required initialization message = "Hello World" ; } public void doGet ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { // Set response content type response . setContentType ( "text/html" ); // Actual logic goes here. PrintWriter out = response . getWriter (); out . println ( "<h1>" + message + "</h1>" ); } public void destroy () { // do nothing. } }

Servlet Life Cycle

Servlet Life Cycle  The Servlet Life cycle can be defined as the entire process from it's creation till the destruction. The following are the are the path followed by the servlets -> The servlet is initialized by calling the init() method . -> The servlet called service method to handle client request -> Then the servlet terminated by calling destroy() method -> Finally servlet is garbage collected by the garbage collector of the JVM Now let's consider the servlet life cycle  in detail INIT() Method The init() method designed to called only once. It is called when the servlet is created and not called over and over and again when the user is requested for the same servlet. The init() method definition looks like this: public void init() Throws ServletException{  // write the initialization code here } The Service method The service method is the main method which helps to perform the actual task . The servlet container call the s

Introduction to Java Servlets

In order to understand the advantages of servlets , you must have the basic under standing of how web browser and web servers are cooperate to provide content to the client . For sake of argument , consider a request for a static web page. The user enters a Uniform Resource Locator( URL) . Then the browser generate a HTTP request for the appropriate server ; the server redirect to the request to the index file of the specified web site . That file return a HTTP response to the client .HTTP header in the HTTP response indicates that the content type of the response. Multipurpose Internet Mail Extension(MIME) is used to the same purpose. for example HTML(Hypertext Markup language ) have a mime type of text/html        In the early days of web, the web server construct a dynamic web page by creating separate process to handle simultaneous request from the client .the process need to open one or more connection to the database to obtain the necessary information. It is communicate wit