Servlet 3

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

Types of communication

======================
We can communicate to servlet program in three ways.

1) Browser to servlet communication

2) HTML to servelt communication

3) Servlet to servlet communication

In browser to servlet communication we need to type our request url in browser


address bar.But typing request url in browser address bar is quit complex.

To overcome this limitation we need to use HTML to servlet communication.

In HTML to servlet communication we can send the request to servlet program by


using HTML based hyperlinks and form pages.

A request which is generated by using hyperlink does not carry the data.

A request which is generated by using form page will carry the data.

In HTML based hyperlink to servlet communication we need to type our request url as
href url.
ex:
<a href="http://localhost:2525/DateApp/test"> click </a>

In HTML based form page to servlet communication we need to type our request url as
action url.
ex:
<form action="http://localhost:2525/DateApp/test">
-
-
-
</form>

Example application on HTML based hyperlink to Servlet Communication


====================================================================
Diagram: servlet3.1

Deployment Directory Structure


----------------------------
WishApp
|
|---Java Resources
|
|-------src
|
|---com.ihub.www
|
|---WishSrv.java
|---WebContent
|
|---index.html
|
|---WEB-INF
|
|---web.xml
Note:
-----
In above application we need to add "servlet-api.jar" file in project build path.

Note:
----
It is never recommanded to extends a class with GenericServlet class because it
won't give HTTP protocol features.

It is always recommanded to extends a class with HttpServlet class because it gives


HTTP protocol features.

index.html
----------
<center>
<h1>
<a href="test"> getMsg </a>
</h1>
</center>

web.xml
-------
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<servlet>
<servlet-name>WishSrv</servlet-name>
<servlet-class>com.ihub.www.WishSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WishSrv</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>

WishSrv.java
-----------
package com.ihub.www;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;

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

public class WishSrv extends HttpServlet


{
public void service(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");

Calendar c=Calendar.getInstance();
int h=c.get(Calendar.HOUR_OF_DAY);

if(h<12)
pw.println("<center><h1> Good Morning </h1></center>");
else if(h<16)
pw.println("<center><h1> Good Afternoon </h1></center>");
else if(h<20)
pw.println("<center><h1> Good Evening </h1></center>");
else
pw.println("<center><h1> Good Night </h1></center>");

pw.close();
}
}

Request url
----------
http://localhost:2525/WishApp/

Example application on HTML based form page to Servlet communication


====================================================================
Diagram: servlet3.2

Deployment Directory Structure


-------------------------------
VoteApp
|
|---Java Resources
|
|-------src
|
|----com.ihub.www
|
|---VoteSrv.java
|---WebContent
|
|---form.html
|
|---WEB-INF
|
|---web.xml
Note:
----
In above application we need to add "servlet-api.jar" file in project build path.

We can give the request to servlet program in two methodologies.

1) GET methodology
----------------
It carries limited amount of data.

2) POST methodology
-------------------
It carries unlimited amount of data.
While extending a class with HttpServlet class , it is never recommanded to use
service(-,-) method because it is not designed according to HTTP protocol.

It is always recommanded to use doXxx(-,-) methods because they have designed


according to HTTP protocol.

We have seven doXxx(-,-) methods in servlet as given below.


ex:
doGet(-,-)
doPost(-,-)
doPut(-,-)
doDelete(-,-)
doOption(-,-)
doTrace(-,-)
doHead(-,-)

prototype of doXxx(-,-) method


------------------------------
protected void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{

form.html
---------

<form action="test" method="GET">

Name: <input type="text" name="t1"/> <br>

Age: <input type="text" name="t2"/> <br>

<input type="submit" value="vote"/>

</form>

web.xml
-------
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<servlet>
<servlet-name>VoteSrv</servlet-name>
<servlet-class>com.ihub.www.VoteSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VoteSrv</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>form.html</welcome-file>
</welcome-file-list>
</web-app>

VoteSrv.java
------------
package com.ihub.www;

import java.io.IOException;
import java.io.PrintWriter;

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

public class VoteSrv extends HttpServlet


{
protected void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");

//reading form data


String name=req.getParameter("t1");
String sage=req.getParameter("t2");

//convert string age to int age


int age=Integer.parseInt(sage);

if(age<18)
pw.println("<center><h1 style='color:red'>"+name+" U r not
eligible to vote </h1></center>");
else
pw.println("<center><h1 style='color:green'>"+name+" U r eligible
to vote </h1></center>");

pw.close();
}
}

Request url
-----------
http://localhost:2525/VoteApp/

Servlet to Database Communication


=================================
Diagram: servlet3.3

Deployment Directory structure


----------------------------
DBApp
|
|---Java Resources
|
|-----src
|
|---com.ihub.www
|
|---DBSrv.java
|---WebContent
|
|---form.html
|
|---WEB-INF
|
|------web.xml
|
|------lib
|
|---ojdbc14.jar
Note:
-----
In above application we need to add "servlet-api.jar" and "ojdbc14.jar" file in
project build path.

Copy and paste "ojdbc14.jar" file inside "WEB-INF/lib" folder seperately.

student table
=============
drop table student;

create table student(sno number(3),sname varchar2(10),sadd varchar2(12));

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy