Sie sind auf Seite 1von 28

Java Web

Application
Development
Java EE 5
Handling A Servlet
Creating a Generic Servlet
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 2
class TheServlet extends GenericServlet
{
int xyz; // The Servlet properties

public void init(ServletConfig config)throws ServletException {
// code to set the initial configuration for servlet
}
public void service(ServletRequest request , ServletResponse response )throws
ServletException,IOException
{
// code to understand the request and generate response
}
public void destroy()
{
// code to do end up process when servlet dies
}

}//end class
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 3
Creating HTTP Servlet
class TheServlet extends HttpServlet
{

public void doGet(HttpResponse resp, HttpRequest req)throws
ServletException , IOException
{
// code to handle get request
}

public void doPost(HttpResponse resp, HttpRequest req)throws
ServletException , IOException
{
// code to handle post request
}

}//end class
TheServlet
is the
resource
name
Hello World Servlet
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 4
class TheServlet extends HttpServlet
{

public void doGet(HttpResponse resp, HttpRequest req)throws
ServletException , IOException
{
PrintWriter out = resp.getWriter();
resp.setContentType(text/html);

out.println(Hello );
out.println(<H3>Hello </H3>);
out.println(<b>Hello </b>);


}// end doGet
}//end class
This method
generate response
on URL request
This object helps to
write information on
browser
Output
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 5
User Request
Location of the server
Name of the application
The Application resource name
Server
Process is called sending
request
Process is called generating response
Handle Request by submitting
HTML form
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 6
Which method to invoke and mention resource name
How it works
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 7
Submitting form using Get method
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 8


The Benefit of Using Post & Drawback of using doGet()
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 9
doGet & doPost differences
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 10
Both are the http protocol methods


doGet() doPost()
Use to transfer non secure
information
Use to transfer secure information.
Such as password
Relatively fast data transmission
Data transmission is slow because
data is transferred through separate
socket connection.
Limited amount of data can be
transferred at once (255 chars)
Unlimited amount of data can be
transferred at once
Clicking on Hyperlink or sending
request by url should be handled
by doGet( )
Clicking on submit button should be
handled by doPost( )
Creating Login Form using get
method
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 11
The Problem
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 12
The submission
of form
Sending request
Reading request values
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 13
Read input values
or
request values
or
request param
?
The Form
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 14
The Servlet Code
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 15
The Output
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 16
Server
Execute doPost()
Example :
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 17
The Servlet
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 18
Handling multiple buttons
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 19
The Servlet Code
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 20
The web.xml
It is also called deployment descriptor.
It describes how the web application should be deployed.
It provides the configuration information to the server.
It is used to map the resources and setting some constant values.
It is written in XML .
The purpose of web.xml are following
what servlets (and filters) you want to use and what URLs you want to map
them to.
listeners - classes that are notified when some events happen.
configuration parameters (context-params).
error pages, welcome files.
security constraints.

MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 21
WEB.XML
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 22
url name of the resource
The Request handler class(Servlet)
Creating web.xml
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 23
<web-app>














</web-app>
<servlet>
<servlet-name>
<servlet-class>
</servlet>
</servlet-name>
</servlet-class>
<servlet-mapping>
<servlet-name>
<url-pattern>
</servlet-mapping>
<servlet-name>
</url-pattern>
Actual class
Which will be
invoked
Set the url
pattern
XYZ
XYZ
p1.MyServletClass
/ABC
How it works
MKJIT-Solutions.com kirti.sharma@mkjit-
solutions.com 24
Hello Servlet
How to manage different files
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 25
The Common directory structure of JEE application for all servers
MyApplication
WEB-INF
classes
*.class
lib
*.jar
Web.xml
Web
*.HTML, *.JSP , *.JPG , *.mp4 , etc
The Complete Architecture
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 26
Java Based Server

web.xml
Data-
base
XYZ.HTML
Thread
req & resp
MyServlet.class
MyLogic.class
1
Send req for
any web resource
& server will listen
its request
2
3
4
5
Response is rendered to
the User & thread dies.
view
controller
Model
The Complete Code
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 27
<html>
<form method=get action=XYZ>
Enter Name
<h:input type=text name=myname/>
<h:input type=submit value=Click/>
</form>
</html>
<web-app>
<servlet>
<servlet-name> hello </servlet-name>
<servlet-class>p1.MyClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> hello </servlet-name>
<url-mapping> /XYZ</url-mapping>
</servlet-mapping>
</web-app>
package p1;

import .
class MyClass extends
HttpServlet
{

public void doGet(
resp, req) throws ----
{
// some code

}//end doGet
}//end class
Reading multiple values from req.
MKJIT-Solutions.com
kirti.sharma@mkjit-solutions.com 28

Das könnte Ihnen auch gefallen