Wednesday, 9 June 2010

Example program on Servlet Request


index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Servlet Request</title>
<form method="get" action="check">
Enter your name<input type="text" name="name"> <br>
<input type="submit" value="login">
</form>
</head>
<body>
</body>
</html>


Servlet Program:
//DemoReq1.java

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoReq1 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
  res.setContentType("text/html");
  PrintWriter pw=res.getWriter();
 
  String name=req.getParameter("name");//will return value
  pw.println("Namasthe  "+name);
  pw.close();
 }
}

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
 <servlet-name>Raj</servlet-name>
 <servlet-class>DemoReq1</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Raj</servlet-name>
 <url-pattern>/check</url-pattern>
</servlet-mapping>
</web-app>










rereree














You may like the following posts:

Tuesday, 8 June 2010

What is web.xml file

The deployment descriptor is an xml file, from which Web Container gets the information about the servet to be invoked.
The web container uses the Parser to get the information from the web.xml file. There are many xml parsers such as SAX, DOM and Pull.
There are many elements in the web.xml file. Here is given some necessary elements to run the simple servlet program.


<web-app> represents the whole application.
<servlet> is sub element of <web-app> and represents the servlet.
<servlet-name> is sub element of <servlet> represents the name of the servlet.
<servlet-class> is sub element of <servlet> represents the class of the servlet.
<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.
<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to invoke the servlet.


Monday, 7 June 2010

Introduction to Servlets

There are two types of Web Resource programs:

1. Static Web resource Programs:
    It generates static web pages. Ex: html pages
2. Dynamic Web resource programs:
    It generates dynamic web pages.
    Ex:
    Servlet programs
    JSP programs
   ASP.net
   Etc.,


What is Web Application?
A Web application is a collection of web resource programs.

img file, java script file can not generate web pages directly but they act as helper programs to another web resource programs.

Web Application is a collection of static, dynamic and helper (Ex: image files) programs.



To Execute Java Apps we need JRE+JVM
To execute Applet programs we need Applet viewer
To execute Servlet programs we need Servlet Container or Web Container. Web Servers (Ex: Tomcat, WebLogic) supplies this Servlet, JSP containers.


Servlet Features:

Better performance: because it creates a thread for each request not process.
Portability: because it uses java language.
Robust: Servlets are managed by JVM so we don't need to worry about memory leak, garbage collection etc.
Secure: because it uses java language..


You may like the following posts:

Friday, 4 June 2010

Prerequisite to learn Servlets


1. Download any server (Ex: Tomcat, Weblogic,..)
    How to download Tomcat Server
2. Download the jar files
    servlet-api.jar
3. JDK 7
4. Eclipse IDE for Java EE Developers (Juno 4.2)
5. import javax.servlet and javax.servlet.http packages

Table of Content:

1. Create Dynamic Web Project

2. Create Servlet class

3. Deploy the servlet

4. Run and test the servlet





How to download Tomcat Server

http://crunchify.com/step-by-step-guide-to-setup-and-install-apache-tomcat-server-in-eclipse-development-environment-ide/

For Command Prompt:
1. go https://tomcat.apache.org/download-80.cgi or

2. Go to Binary Distributions
3. Choose Core:
4. Click on 32-bit/64-bit Windows Service Installer (pgp, md5, sha1)
5. Once you download the Tomcat Server installer,click on to install

Go to Downloads in your PC


Click on apache-tomcat-..exe file

Next





HelloWorld servlet program