Sunteți pe pagina 1din 6

SERVLETS [ST I]

Apache-Tomcat installation steps: --------------


Insure that JDK is installed and running successfully.
Click on F:\Student cd1\Apache Tomcat 5.0\jakarta-Tomcat 5.0.16.
Click on next
Click on I agree button.
Click on next button.
Installation directory -------------C:\Tomcat 5.0 click on next button. Provide the port no. as 9999/8080 and user
name as jlc and password as rajiv and click on next button.
Verify the JVM path ok means click on install button.
Unchecked the two checked boxes and click on finish button. Tomcat installation completed successfully.
Starting the Tomcat Server: ----------------
Click on start -------All program ------ Apache Tomcat 5.0 ---- start Tomcat --- started Tomcat Webserver
Verifying the Tomcat :---------
Open the browser and type the following :---- http://localhost:8080/ -------- html page of verification
st
Writing 1 servlet program :--------------

Register.html RegisterServlet.java
Java learning center
Registration form  Collect the data given by the user

Name  Prepare the message

Email  Send the response to the user.

Phone

Qualification
Requesting
Gender male female Response
Java Learning Center
Hello !!!!!!
Register Me
You name is : Rajiv shyam
Your Email is : rajivsh101@gmail.com
Your phone is : 9343313122
Files Required :--------- Your qualification is : BE
 Register .html
 RegisterServlet.java
 Web.xml
Copy the Register.html into Root directory.
Set the classpath as follows :--------
Set classpath=%classpath%;c:\Tomcat5.0\common\lib\servlet-api.jar;
 Compile the RegisterServlet.java class.
 Copy the com folder to classes folder.
 Update the web.xml
 Start the Tomcat Server
Open the Browser and type the following :----------
http://localhost:8080/Register.html
Enter all the data in html program and click on register me button and see the results of the program.
C [ST II]

bi Tomcat 5.0
n

Webapps
conf Commo
n
*.html
Root *.jsp
Register.html
Lib
*.jar files
Servlet-api.jar WEB-INF

Web.xml
Classes

Commo
n

RegisterServlet().class Jlc
Servlet
s

When you hits the browser with some URL 1st request will be given to DNS server .in DNS server domain will
be resolved of IP address and request will be go to the server where application is running.
Once server receive the request it process the request and send the response to the client .server identify the
client IP address from the Http request.
There are two component are involving:-----------
• Web client
• Web server
Web client is a software which sends the request to web server and also responsible to receive the response and
display to the client . example :-----Mozila , opera ,safari

Web server is a software which receive the request from web client and process the request and send the
response to web client. Example :--- java server, apache tomcat, Web logic ,oracle 9i,,orient server matrix
server ,JBOS,JRUN,PWS,IIS(internet information server)
In the World Wide Web the communication between web client and web server is happing with http protocol.
The request what one you sending with using http is called HttpRequest.
Http request has two part: -------
• HttpRequesr head
• HttpRequest body
HttpRequest [ST III]
Header
GET WWW.gmail.com /login html? User_name sri & password sri
Host: www.gmail.com
Accept language : US English
Accept –encoding : .gzip
Connection : keep Alive
---------------
--------------- // BODY
---------------

You can send the HttpRequst to web server using one of the following Http methods :----------
 GET
 POST // GET & POST are important only
 PUT
 DELETE
 OPTIONS
 TRACE
 HEAD
When you send request using GET method data will attach to URL where as when you send the request using
POST method the data will go to the server along with Http request body.

HttpResponse
HttpResponse
Decoder

Status code : 100,101,200,201,------ 400, 401 ------- 500,501

Location : close

Body

<html>

</html>

HttpResponse body always contains html code which can be understood by any web browser some times
HttpResponse body contains image, PDF, document(MS-Word), MS-Excel documents etc.
HttpResponse body allows various content type this type are called as MIME(multipurpose internet mail
extensions ) type .
Http is responsible to carry the Hypertext from client to server or server to client.
Http sits on top of TCP/IP .
IP is the protocol which carry the data pocket from one machine to another machine.
TCP just controls the data transmission i.e. TCP checks whether all the data packet started from client are
reached the server or not and vice-versa .
I want to develop some web page application what technology you can suggest ?
Servlets,JSP,PHP,Asp,.NET, Struts ,JSF, Spring
D/F between GET and POST [ST IV]
GET POST
 When you send the request using GET method  When you send the request using POST
then data will be attached to the URL as query method then data will send to Server along
string .In this case HttpRequest body is empty. with HttpRequest body.
 Using GET we can send only limited amount  Using POST you can send unlimited amount of
of data . data.
 Because of attaching the data in URL user can  POST is very secure because user can’t see the
see the data so you can say GET is not secure. data .

Don’t forget to do this things before execute any Servlet programs

1st of all you write three files in any drives as you like
 Register .html Carefully you handle these things
 RegisterServlet.java
 Web.xml
Set classpath as follows in environment variable: --------- permanent setting of classpath
Name classpath
Value %classpath%;F:\jar\class111.jar;F:\jar\mysql.jar;F:\jar\servletapi.jar;F:\jar\htmlconverter.jar;
F:\jar\tools.jar;F:\jar\jsp-api.jar;
You can set the classpath for many languages like mysql, servlets ,jsp and many more only careful about you
can separate this with semicolon(;).
 Search servlet-api.jar and past it to C:\Tomcat 5.0\common\lib
 Compile java file(RegisterServlet.java)as follows F:\>javac -d . RegisterServlet.java
 After compilation create a class file called as RegisterServlet.class in the F:\com\jlc\servlets
Copy this com folder and past it to C:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes
 Copy web.xml to C:\Tomcat 5.0\webapps\ROOT\WEB-INF
 Copy the html (Register.html) file in C:\Tomcat 5.0\webapps\ROOT folder
Start the Tomcat5.0 sever
Type the following in web Browser
http://localhost:8080/Register.html
Entered all the data and get it to Register and show the respective result for your program .
 Here you more careful
• In Html
<form action="/Register.do">
• In Xml
<url-pattern>/Register.do</url-pattern>
<servlet-class>com.jlc.servlets.RegisterServlet</servlet-class>
Here com.jlc.servlet.RegisterServlet is the content of com folder which you will get after compilation of
servlet.java (RegisterServlet)program.
1st Servlet program Register.html [ST V]
<html> <select name="quali">
<head> <option value="M.Sc">M.Sc</option>
<title>JLC</title> <option value="M.Sc">M.C.A</option>
</head> <option value="M.Sc">M.B.A</option>
<body bgcolor="sky blue"> <option value="M.Sc">B.Tech</option>
<br><br><center> <option value="M.Sc">B.A</option>
<u><h1>JAVA LEARNING CENTER</h1></u> </select>
<form action="/Register.do"> </td>
1st servlet program
<table> </tr> RAMAN SHYAM
<tr> <tr>
<td>NAME</td> <td>Gender</td>
<td><input type="text" name="name"/></td></tr> <td><input type="radio"name="gender"
<tr> value="Male"/>Male
<td>Email</td> <input type="radio"name="gender"
<td><input type="text" name="email"/></td> value="Female"/>Female
<tr> </td></tr>
<td>PHONE</td> <tr>
<td><input type="text" name="phone"/></td></tr> <td colspan="12" align="center">
<tr> <input type="submit" value="REGISTER ME"/>
<td>QUALIFICATION</td><td> </td></tr>
</table> </form> </center> </body> </html>

RegisterServlet.java
package com.jlc.servlets; String ph=req.getParameter("phone");
import java.io.*; String qu=req.getParameter("quali");
import java.util.*; String ge=req.getParameter("gender");
import javax.servlet.*; //prepare message
import javax.servlet.http.*; String msg="HELLO!!!!"+n+"WELCOME TO JLC-------";
public class RegisterServlet extends HttpServlet //send the responseres.setContentType("text/html");
{ PrintWriter out=res.getWriter();
public void service(HttpServletRequest out.println("<h1>"+msg+"<br>");
req,HttpServletResponse res) out.println("you Name is :"+n+"<br>");
throws ServletException,IOException out.println("your Email id is:"+em+"<br>");
{ out.println("your Phone number is "+ph+"<br>");
//collect the data out.println("your qualification is :"+qu+"<br>");
String n=req.getParameter("name"); out.println("your gender is :"+ge+"<br>");
String em=req.getParameter("email"); }
}
Web.xml

<?xml version="1.0" encoding="ISO-8859-1"?> <servlet-mapping>


<!DOCTYPE web-app <servlet-name>Register</servlet-name>
PUBLIC "-//Sun Microsystems, Inc.//DTD Web <url-pattern>/Register.do</url-pattern>
Application 2.3//EN" </servlet-mapping>
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app> </web-app>
<servlet>
<servlet-name>Register</servlet-name> 1st SERVLET Program
<servlet-class>com.jlc.servlets.RegisterServlet</servlet-class> RAMAN SHYAM
</servlet> BY JLC

S-ar putea să vă placă și