Sei sulla pagina 1di 10

PASSING AND RETRIEVING

PARAMETERS

Servlet is a server side version of an applet


Their are situations when we need to pass some information from
the browser to web server and ultimately to your backend program.
The browser uses two methods to pass this information to web
server. These methods are GET Method and POST Method.
Servlet handles this type of requests using doGet() method and
the doPost() method.
The syntax of the doGet method is as follows:
Protected void doGet
(HttpServletRequest request,HttpServletResponse response)
throws ServletException, java.io.IOException
ServletException is a subclass of Exception that serves as a wrapper for
every kind of general servlet problem.
In the syntax shown above, request and response are the names of the
reference variables for the request and response objects.
The HttpServletRequest object parameter, request, contains the client
request
The HttpServletResponse object parameter, response,provides the
means to communicate the response that the servlet sends back to the
client.
The syntax of the doPost() method is same as that of doGet.
Servlet output to the requesting client is created by defining PrintWriter
object using the getWriter method.
The printwriter provides a collection of methods such as the println.
Before the printwriter object is created it is essenial that the content type
of the return document to be set. This is done witn the setContentType
method.
Example of GET method
<html xmlns = http://www.w3.org/1999/xhtml>
<head> <title> test greeting </title></head>
<body>
<form action = http://localhost/servlet/Greeting method = get>
<p>
Press button
<input type = submit value = enact servlet />
</p>
</form> </body> </html>

output
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Public class Greeting extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(text/html);
PrintWriter result = response.getWriter();
result.println(<html><head><title>);
result.println(</title></head><body>);
result.println(<h2>this is your servlet</h2>);
result.println(</body></html>);
result.close();
}}
output

This is your servlet

Example for POST

<html>
<head>
<title> post </title> </head>
<body>
<form action=http://localhost/servlet/user method=post>
Name:<input type=text name=uname/>
Password:<input type=password name=pass/>
<input type=submit value=login/>
</form> </body></html>
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Public class User extends HttpServlet {
Public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user=req.getParameter(uname);
String password=req.getParameter(pass);
PrintWriter pw=res.getWriter();
pw.println(<html> <body>);
pw.println(welcome+user+<br/>);
pw.println(your password +password+);
pw.println(</body></html>);
}}
output

Welcome A
Your password xyz
Security Issues
Interception of Session State Information
Forgery of Session State Information
Session Timeout
Buffer Overflow
Data Validation
Page Sequencing
User Authentication
Logging of Sensitive Information
THANK YOU

Potrebbero piacerti anche