Professional Documents
Culture Documents
Servlets
Servlets
Servlets
1. What is Component?
A Web component on the server-side is the one invoked by the client using
Web (Internet) as medium (interface) of communication.
4. What is Servlet?
image
<body>
</form>
</body>
<form method="get" action="http://localhost:8888/india/roses">
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Being the first program, let us have a detailed discussion on each line.
1. import javax.servlet.*;
2. import javax.servlet.http.*;
The above package is imported for the abstract class
HttpServlet and interfaces HttpServletRequest and HttpServletResponse.
3. import java.io.*;
image
a) HttpServletRequest interface
image
b) HttpServletResponse interface
image
6. res.setContentType("text/html");
10. out.close();
When the job is over, it is customary to close the streams as you have
done in file copying.
http://tomcat.apache.org/download-60.cgi
Check with the documentation of what JDK or JRE version is compatible to
the specific Tomcat version. For example I have loaded Tomcat 5.0 and is
compatible with JDK 6.0.
While installation, it asks the port number and I have entered 8888
(default is displayed as 8080. I have not preferred 8080 for the reason
on many systems Oracle server will be working on 8080. For this reason
better choose a different port number). Later, give your own password.
Step 1:When installed, Tomcat gives many folders of which a few are given
hereunder used for execution. See the following one.
Note: Now, a fresher should be careful here in the following steps. Steps
are very simple but should be followed carefully. Any small mistake
committed, Tomcat simply refuses to execute your servlet.
In the above webapps folder create your own new folder. I created and
named it as "india".
Just copy the above WEB-INF folder (ofcourse, along with its
subdirectories) into india folder.
When you did, now you get the following structure. Check it.
Note 1: Do not confuse now with the WEB-INF folder as WEB-INF exists in
two places – one in ROOT and one in india.
Remember, now onwards when I talk about WEB-INF folder, I mean the WEB-
INF available in india and not in ROOT. This is very important.
Step 5: Giving the alias name to the servlet with Deployment Descriptor.
In the web.xml file add the following code just before </web-app> tag.
<servlet>
<servlet-name>abcd</servlet-name>
<servlet-class>Validation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abcd</servlet-name>
<url-pattern>/roses</url-pattern>
</servlet-mapping>
Now the alias name of Validation servlet is roses. Client should call the
Validation servlet with the name roses. Observe, this roses you used in
HTML file.
Step 7: Open a browser. Open the UserPass.html file from File Menu of the
browser, enter the user name and password and click Send button. Now you
get VALID or INVALID.
Note: Whenever the servlet file is copied (even the same one), it is
required to restart the Tomcat every time.
1. What is Deployment?
Copying the .class file of the servlet from the current directory to the
classes folder of Tomcat (or any Web server) is known as deployment. When
deployed, Tomcat is ready to load and execute the servlet, at anytime, at
the client request.
First and foremost is the alias name to the servlet. Never a client is
given the actual name of the servlet. Always an alias name is given just
for security (avoid hacking). The alias name is given in the following
XML tags.
image
The servlet comes with two alias names, internal and external. The
internal name is used by the Tomcat and the external name is given (to be
written in <FORM> tag of HTML file) to the client to invoke the servlet
on the server. That is, there exists alias to alias. All this is for
security. Observe, the names are given in two different XML tags, in the
web.xml file, to make it difficult for hacking (for more security in EJB,
two alias are given in two different XML files).
To invoke the Validation servlet, the client calls the server with the
name roses. When roses call reaches the server, the Tomcat server opens
the web.xml file to check the deployment particulars. Searches such a
<servlet-mapping> tag that matches roses. roses is exchanged with abcd.
Then, searches such a <servlet> tag that matches abcd and exchanges with
Validation. Now the server, loads Validation servlet, executes and sends
the output of execution as response to client.
<init-param>
<param-name>trainer</param-name>
<param-value>S. Nageswara Rao</param-value>
</init-param>
This can be read by the servlet using ServletConfig interface. It is more
discussed in init param Example with ServletConfig.
c) To write tag libraries (this is mostly used in frameworks like Struts
etc)
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
Following are a few important XML elements in web.xml.
Note 2: For every servlet, there must be an entry in web.xml file with an
alias name.
This is the modified code of the Login Screen Validation. In the first
program, the user name and password are read by the servlet, validated
and sent back the result of validation (valid or invalid) as response.
The modification is, whichever field is wrong, that field is sent empty
and which field is correct, that is filled and sent. That is both user
name and password are to be validated separately and as per the
validation, a HTML form is generated dynamically (on the fly) and sent to
the client. This is like creating a new email account.
<body>
<form method="get"
action="http://localhost:8888/india/dynamicgeneration">
Enter User Name <input type="text" name="t1"> <br>
Enter Password <input type="password" name="t2"> <br>
<input type="submit" value="Send">
</form> </body>
In the HTML, there is nothing new. Following is the web.xml entry for the
servlet.
<servlet>
<servlet-name>dhfg</servlet-name>
<servlet-class>DynamicHTMLFormGeneration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dhfg</servlet-name>
<url-pattern>/dynamicgeneration</url-pattern>
</servlet-mapping>
Following is the servlet program.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
if( str1.equals("snrao") )
{
out.println("Enter User Name <input type=text name=t1 value=" +
str1 + "> <br>");
}
else
{
out.println("Enter User Name <input type=text name=t1> <br>");
}
if( str2.equals("java") )
{
out.println("Enter Password <input type=password name=t2 value="
+ str2 + "> <br>");
}
else
{
out.println("Enter Password <input type=password name=t2>
<br>");
}
}
out.close();
}
}
When both fields are given correct values as follows
ima
ima
ima
ima
<body>
</form>
</body>
Servlet Program: File Name: Validation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
ima
ima
ima
ima
<body>
<form method="get" action="http://localhost:8888/india/readall">
</form>
</body>
Observe, there are 6 form fields from t1 to t6. The aim of this servlet
is to read all at a time.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>GetPNames</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/readall</url-pattern>
</servlet-mapping>
Servlet: GetPNames.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Enumeration;
Enumeration e = req.getParameterNames();
while(e.hasMoreElements())
{
Object obj = e.nextElement();
String fieldName = (String) obj;
String fieldValue = req.getParameter(fieldName);
out.println(fieldName + " : " + fieldValue + "<br>");
}
out.close();
}
}
Enumeration e = req.getParameterNames();
The e object of Enumeration contains all the names of fields like t1, t2,
t3 etc. but not their values entered by the user. The nextElement()
returns one field name for each iteration. The field name is passed to
getParameter(fieldName) method to retrieve the value entered by the user.
ima
ima1
In the first simple program Login Screen Validation, the data sent by the
client is extracted from HttpServletRequest object req, using
getParameter(String) method. The getParameter(String) method works to
read a single text box (or any FORM field) only. The HttpServletRequest
interface comes with another method getParameterNames() (inherited from
ServletRequest interface) to read all the FORM fields’ values at a time.
Following is the method signature.
As usual there exists minimum two programs running one on client-side and
the other on server-side.
<body>
<H3> Reading all Text box values at a time </H3>
<form method="get" action="http://localhost:8888/india/tiger">
</form> </body>
Observe, the above HTML file comes with five text boxes from t1 to t5.
The following servlet, reads all at a time.
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.io.*;
while(e.hasMoreElements())
{
String name=(String) e.nextElement();
out.println( name +" : ");
String value=req.getParameter(name);
out.println(value.toUpperCase() +"<br>");
}
out.println("</body>");
out.close();
}
}
web.xml entry for the above servlet:
<servlet>
<servlet-name>animal</servlet-name>
<servlet-class>ReadAll</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>animal</servlet-name>
<url-pattern>/tiger</url-pattern>
</servlet-mapping>
String str1 = req.getParameter ("t3");
The above method is known earlier in the first program and with this
method, we learnt how to read a single text box value. The same is
repeated with t3 text box to know the value entered by the user in t3
box. But the aim of this code is different – to read all boxes’ values at
a time.
Enumeration e = req.getParameterNames();
The name variable contains the name of the FORM field (like t1 or t2
etc.). The getParameter(name) returns the value entered by the user in
the FORM field name. The value variable contains the actual data entered
by the user in the field name.
Using Enumeration object, all the values entered by the user in the FORM
fields are extracted.
ima
ima
Observe the response screen. The values are not in the other of t1 to t5.
It is the problem with Enumeration iteration and not with servlet.
Anyhow, the order is not important here.
Servlets getParametehttp://way2java.com/jsp/first-jsp-example-user-name-
and-password-login-validationrValues() Example -:::::
First let us see what Servlet API says about these methods. These are
defined in javax.servlet.ServletRequest interface.
<body>
<form method="get" action="http://localhost:8888/india/readall">
</form>
</body>
Observe, there are total 7 form fields of which five fields are having a
common name of t3. All the five form fields with t3 name comprises of a
student address. The job of the servlet may be to extract all the 7 form
fields data and enter into a database table. Imagine, the database have
only 3 columns like stdname, stdid and stdaddress. The value of t1 goes
into the stdname column, t2 goes into stdid and all the remaining five
fields with name t3 goes into stdaddress.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>GetPNames</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/readall</url-pattern>
</servlet-mapping>
Servlet: GetPNames.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.println(temp);
out.close();
}
}
getParameterValues(String) avoids reading separately. But the condition
is all the fields should have the common name. The StringBuffer object
temp can be taken and inserted into a database column.
ima
ima1
Let us see what Java API says about these methods as defined in
javax.servlet.ServletRequest interface and inherited by
HttpServletRequest interface.
The developer should specifiy to the browser in what style the data is to
be sent to the server. The most commonly used styles are GET and POST.
These styles are to be written in METHOD attribute of <FORM> tag as
follows.
Remember the Login Screen Validation Servlet. The screens are displayed
here in relevance to GET style.
Observe the client HTML program when the fields (text boxes) t1 (for user
name) and t2 (for password) are filled up.
ima
http://localhost:8888/india/roses?t1=S+N+Rao&t2=java
The data of user name (S N Rao) and the password (java) are appended to
the URL with a delimiter (separator) of ?, that is, the actual URL
separated from the client data with a question mark, ?. Still observe,
the spaces in between S N Rao are indicated with + symbol and delimiter &
is used to separate two fields of t1 and t2.
When a client clicks a hyper link or a submit button, we know that the
data entered by the user in the Form fields is sent to server and is the
default behaviour of submit button. Ofcourse, right. But along with a lot
of extra information goes to server on the name of headers (attached to
request object) like what browser client is using, its supported
languages etc.
Apart header data, lot of other data of client system also goes like what
client system IP address is, what protocol client is using and on what
port number client browser is sending request etc.
Let us see first what Servlet API says about these methods:
<body>
</body>
web.xml entry for Headers.java servlet
<servlet>
<servlet-name>snrao1</servlet-name>
<servlet-class>Headers</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>snrao1</servlet-name>
<url-pattern>/head</url-pattern>
</servlet-mapping>
Server Servlet File: Headers.java
import javax.servlet.*;
import javax.servlet.http.* ;
import java.io.* ;
import java.util.*;
public class Headers extends HttpServlet
{
public void service( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Enumeration e = req.getHeaderNames();
out.println("<H3>Following are the Headers coming from the
Client<BR></H3>");
while(e.hasMoreElements())
{
String name = (String) e.nextElement();
String value = req.getHeader(name);
out.println("<tr><th>"+name + "</th><th>" + value + "</th></tr>");
}
out.println("</table>");
out.close();
}
}
Enumeration e = req.getHeaderNames();
The getHeaderNames() returns the header names in the form of strings. For
this reason, the object returned by nextElement() is casted to String.
Pass the header name "name" to getHeader() and the method returns the
associated value as a string. In the while loop, with each iteration, one
header name and its value are printed. Observe the output screen.
ima
ima1
In the above screenshot, the first column of table gives the header names
and the second column gives the values associated with each header.
host: This header gives the host (server) name and port number as written
in the URL (in ACTION attribute).
connection: It specifies whether the client’s browser can handle the
persistent HTTP connections. "keep-alive" indicates that the browser can
handle persistent connections. On a persistent connection, the browser
can retrieve multiple files on a single request (or single connection).
accept: This header informs the Server (or Servlet) what MIME types (like
text/html, image/gif etc.) the browser supports (so that the Servlet can
send response in the supported MIME type only).
user-agent: Returns the name of the browser and its version etc. This
information is used by the Servlet to send data specific to that browser.
Suppose, the Servlet would like to send <MARQUEE> tag and if client is
having Netscape Navigator browser (which does not support MARQUEE tag),
it does not send and instead it may send the same information in <BLINK>
tag supported by Netscape. Or a Servlet sends a response to the browser
that requires an ActiveX control which the browser may not have.
Microsoft IE version 4 does not come with ActiveX controls supported by
version 5. For this reason, the Servlet requires the user-agent
information.
accept-encoding: This header tells the Servlet what type of compression
algorithms are supported by the browser like gzip etc. so that Servlet
sends an appropriate compressed file to the browser.
accept language: This informs the Server that what type of languages the
browser support like English, Spanish or Japanese etc., so that the
Servlet may send response in other languages supported by the browser.
Java and Servlets support Internationalization.
When the user clicks the submit button, we think that the data entered by
the user like user name and password are sent to the server. Ofcourse,
right. But along with it, lot of client data goes to the server. The
client data includes the protocol used by the client to access the
server, client IP address and also the name of the browser client is
using, its version (headers) etc.
This program uses getRequestURL() to retrieve the URL used by the client
to call the servlet on the server.
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Server Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
StringBuffer sb = req.getRequestURL();
out.println("req.getRequestURL() : " + sb);
out.close();
}
}
Output screen of ClientData.html with text field filled up.
getme
ima
When the user clicks the submit button, we think that the data entered by
the user like user name and password are sent to the server. Ofcourse,
right. But along with it, lot of client data goes to the server. The
client data includes the protocol used by the client to access the
server, client IP address and also the name of the browser client is
using, its version (headers) etc.
This program uses getRequestURI() to retrieve the a part of the URL used
by the client to call the servlet on the server.
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Server Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Following is the method signature as defined in HttpServletRequest
interface.
getme
ima
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up.
getme
ima
With this method, the servlet programmer can know the IP address of the
client from which the request came.
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up. Ofcourse,
this does not have any relevance with the method getRemoreAddr() method.
getme
ima
With this method, the servlet programmer can know the name of the client
from which the request came.
java.lang.String getRemoteHost():
Returns the fully qualified name of the client or the last proxy that
sent the request. If the engine cannot or chooses not to resolve the
hostname (to improve performance), this method returns the dotted-string
form of the IP address. For HTTP servlets, same as the value of the CGI
variable REMOTE_HOST.
The method getRemoteHost() returns the name of the client system as a
string.
Let us see the output by writing a program.
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up. Ofcourse,
this does not have any relevance with the method getRemoreAddr() method.
getme
ima
Let us see what Java API says about these methods as defined
javax.servlet.ServletRequest interface.
1
2
3
4
5
6
7
8
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up. Ofcourse,
this does not have any relevance with the methods.
getme
ima
With this method, the servlet programmer can know the data (through FORM
fields like user name etc.) sent by the client.
java.lang.String getQueryString():
Returns the query string that is contained in the request URL after the
path. This method returns null if the URL does not have a query string.
Same as the value of the CGI variable QUERY_STRING.
The method getQueryString() returns a string of data, filled by the user
in FORM fields (of text boxes, check boxes etc.), and sent to server.
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up as SNRao.
getme
ima
Observe, t1 is the name of text box in the HTML file and SNRao is the
value entered in t1. The field and value are separated by = symbol.
With this method, the servlet programmer can know the length of data
(through FORM fields like user name etc.) sent by the client. Or to say,
the length (in bytes) of query string.
It is the string containing the name of FORM fields and the data entered
by the user in the fields. Field is separated with the value entered by
the user with = symbol (see the output screen).
<body>
</body>
Observe, the client uses8888 port number in attribute ACTION.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up as SNRao.
getme
ima
<body>
</body>
web.xml entry for ClientInformation servlet
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Let us go to the coding part.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up.
getme
ima
Observe, the getServletPath() prints just the alias name and not complete
URL. To retrieve complete URL, use getRequestURL() method of
HttpServletRequest.
With this method, the servlet programmer can know what method (either get
or post) is used by the client from HTML file to call the servlet.
<body>
</body>
Observe, the client uses GET value for attribute METHOD.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up. Anyhow, the
text field does not have any purpose here.
getme
ima
With this method, the servlet programmer can know what protocol (like
HTTP/1.0 or HTTP/1.1) is used by the client from HTML file to call the
servlet.
java.lang.String getProtocol():
Returns the name and version of the protocol the request uses in the form
protocol/majorVersion.minorVersion, for example, HTTP/1.1. For HTTP
servlets, the value returned is the same as the value of the CGI variable
SERVER_PROTOCOL.
The method getProtocol() returns a string, the protocol used in ACTION
attribute of of FORM tag used by the client to call the servlet.
<body>
</body>
Observe, the client uses GET value for attribute METHOD.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up. Anyhow, the
text field does not have any purpose here.
getme
ima
With this method, the servlet programmer can know what protocol (like
HTTP) is used by the client from HTML file to call the servlet.
<body>
</body>
Observe, the client uses GET value for attribute METHOD.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up. Anyhow, the
text field does not have any purpose here.
getme
ima
With this method, the servlet programmer can know what port number is
used by the client from HTML file to call the servlet.
int getServerPort():
Returns the port number to which the request was sent. It is the value of
the part after ":" in the Host header value, if any, or the server port
where the client connection was accepted on.
The method getServerPort() returns an integer value, the port number used
in ACTION attribute of of FORM tag used by the client to call the
servlet.
<body>
</body>
Observe, the client uses8888 port number in attribute ACTION.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
out.close();
}
}
Output screen of ClientData.html with text field filled up. Anyhow, the
text field does not have any purpose here.
getme
ima
With this method, the servlet programmer can know whether the client is
using a secured protocol (HTTPS) or not while calling the servlet. If
secured protocol is used, this method returns true else false.
<body>
</body>
Observe, the client uses http in attribute ACTION.
<servlet>
<servlet-name>efgh</servlet-name>
<servlet-class>ClientInformation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>efgh</servlet-name>
<url-pattern>/jasmine</url-pattern>
</servlet-mapping>
Servelet Program: ClientInformation.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
boolean b1 = req.isSecure();
out.println("req.isSecure() : " + b1);
out.close();
}
}
Output screen of ClientData.html with text field filled up. Anyhow, the
text field does not have any purpose here.
getme
Because the client is using http and not https, the output screen above
shows a false value.
Note: Instead of http if https is used in the client program, the browser
tries to establish a secure connection with the server and if unable
throws ERR_SSL_PROTOCOL_ERROR with the message "Unable to make a secure
connection to the server. This may be a problem with the server or it may
be requiring a client authentication certificate that you don’t have."