0% found this document useful (0 votes)
24 views

Servlet 2

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Servlet 2

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

First web application development having servlet program as web resource program

================================================================================
Diagram: servlet2.1

Deployment Directory structure


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

step1:
-----
Launch Eclipse IDE by choosing workspace location.

step2:
-----
Create a dynamic web project i.e DateApp.
ex:
File --> New --> Dynamic web project --> Project Name : DateApp
---> Next --> Next --> select generate web.xml file --> Finish.

step3:
------
Add "servlet-api.jar" file in project build path.
ex:
right click to DateApp --> Build path --> configure build path -->
libraries --> click to classpath --> Add external jars -->
select servlet-api.jar file --> open --> Apply & close.

step4:
-----
Add Tomcat9 server to eclipse IDE.
ex:
window --> preferences --> server --> Runtime Environments -->
click to add button --> select Apache Tomcat 9 --> Next -->
select destination folder --> Finish --> apply and close.

step5:
-----
Create a "com.ihub.www" package inside "JavaResources/src" folder.

ex:
right click to src folder --> new --> package --> Name : com.ihub.www
--> Finish.
step6:
-----
Create a DateSrv.java file inside "com.ihub.www" package.
ex:
right click to com.ihub.www package --> new --> class --> Name :
DateSrv -->
Finish.

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

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

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class DateSrv extends GenericServlet


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

Date d=new Date();

pw.println("<center><h1>Current Date and Time <br> "+d+"


</h1></center>");

pw.close();
}
}

step7:
------
Configure each servlet program in web.xml file.

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>DateSrv</servlet-name>
<servlet-class>com.ihub.www.DateSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DateSrv</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>

step8:
-----
Run dynamic web project.
ex:
right click to DateApp --> run as --> run on server --> select Apache
Tomcat 9
--> next --> Finish.

step9:
------
Test the application by using below request url.
ex:
http://localhost:2525/DateApp/test

Note:
-----
If we do any mistake in web.xml file then we will get 404 Error.
If we do any mistak in servlet program the nwe will get 500 Error.

Types of URL Patterns


=====================
URL pattern will hide technology name or class name from outsiders for security
reason.

Our client, server and other web resources will recognize each servlet program by
using url pattern.

we have three types of url patterns.

Every server is designed to support three types of url patterns.

1) Exact match url pattern

2) Directory match url pattern

3) Extension match url pattern

1) Exact match url pattern


--------------------------
It starts with '/' slash symbol followed by a name.

ex:
web.xml
-------
<url-pattern>/test</url-pattern>

Request url
-----------
http://localhost:2525/DateApp/test (valid)
http://localhost:2525/DateApp/best (invalid)
http://localhost:2525/DateApp/temp (invalid)

2) Directory match url pattern


-------------------------------
It starts with '/' symbol and ends with '*' symbol.

ex:
web.xml
-------
<url-pattern>/x/y/*</url-pattern>

Request url
-----------
http://localhost:2525/DateApp/x/y/z (valid)
http://localhost:2525/DateApp/x/y/z/test (valid)
http://localhost:2525/DateApp/y/x/z (invalid)

3) Extension match url pattern


----------------------------
It starts with '*' symbol having some extension.

ex:

web.xml
-------
<url-pattern>*.do</url-pattern>

Request url
-----------
http://localhost:2525/DateApp/test (Invalid)
http://localhost:2525/DateApp/test.do (valid)
http://localhost:2525/DateApp/y/x/z.do (valid)

MIME Type
===========
MIME stands for Multipurpose Internet Mail Extension.

MIME describes in which format we want to print the output in servlets.

We can display servlet output in following formats.

1) text/html
------------
It is used to display the output in html format.

2) text/xml
----------
It is used to display the output in xml format.

3) application/ms-word
-------------------
It is used to display the output in word format.

4) application/vnd.ms-excel
----------------------------
It is used to display the output in excel format.

Deployment Directory Structure


------------------------------
MIMEApp
|
|---Java Resources
| |
|------src
|
|---com.ihub.www
|
|---TestSrv1.java
|---TestSrv2.java
|---TestSrv3.java
|---TestSrv4.java
|---WebContent
|
|---WEB-INF
|
|---web.xml
Note:
-----
In above application we need to add "servlet-api.jar" file in project build path.

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

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

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TestSrv1 extends GenericServlet


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

pw.println("<table border='1'>");
pw.println("<tr><th>No</th><th>Name</th><th>Add</th></tr>");
pw.println("<tr><td>101</td><td>Alan</td><td>Texas</td></tr>");
pw.println("<tr><td>102</td><td>Jose</td><td>Florida</td></tr>");
pw.println("<tr><td>103</td><td>Mark</td><td>Miami</td></tr>");
pw.println("</table>");

pw.close();

}
}

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

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TestSrv2 extends GenericServlet


{
public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/xml");

pw.println("<table border='1'>");
pw.println("<tr><th>No</th><th>Name</th><th>Add</th></tr>");
pw.println("<tr><td>101</td><td>Alan</td><td>Texas</td></tr>");
pw.println("<tr><td>102</td><td>Jose</td><td>Florida</td></tr>");
pw.println("<tr><td>103</td><td>Mark</td><td>Miami</td></tr>");
pw.println("</table>");

pw.close();

}
}

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

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

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TestSrv3 extends GenericServlet


{
public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("application/ms-word");

pw.println("<table border='1'>");
pw.println("<tr><th>No</th><th>Name</th><th>Add</th></tr>");
pw.println("<tr><td>101</td><td>Alan</td><td>Texas</td></tr>");
pw.println("<tr><td>102</td><td>Jose</td><td>Florida</td></tr>");
pw.println("<tr><td>103</td><td>Mark</td><td>Miami</td></tr>");
pw.println("</table>");

pw.close();

}
}

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

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

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TestSrv4 extends GenericServlet


{
public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("application/vnd.ms-excel");

pw.println("<table border='1'>");
pw.println("<tr><th>No</th><th>Name</th><th>Add</th></tr>");
pw.println("<tr><td>101</td><td>Alan</td><td>Texas</td></tr>");
pw.println("<tr><td>102</td><td>Jose</td><td>Florida</td></tr>");
pw.println("<tr><td>103</td><td>Mark</td><td>Miami</td></tr>");
pw.println("</table>");

pw.close();

}
}

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>TestSrv1</servlet-name>
<servlet-class>com.ihub.www.TestSrv1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestSrv1</servlet-name>
<url-pattern>/html</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>TestSrv2</servlet-name>
<servlet-class>com.ihub.www.TestSrv2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestSrv2</servlet-name>
<url-pattern>/xml</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>TestSrv3</servlet-name>
<servlet-class>com.ihub.www.TestSrv3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestSrv3</servlet-name>
<url-pattern>/word</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>TestSrv4</servlet-name>
<servlet-class>com.ihub.www.TestSrv4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestSrv4</servlet-name>
<url-pattern>/excel</url-pattern>
</servlet-mapping>

</web-app>

Request url
---------
http://localhost:2525/MIMEApp/html
http://localhost:2525/MIMEApp/xml
http://localhost:2525/MIMEApp/word
http://localhost:2525/MIMEApp/excel

You might also like