answersLogoWhite

0

JSP and Servlets

JSP (JavaServer Pages) are Java programs encoded within webpages that research and generate pertinent results. Servlets, on the other hand, are objects (programs) that handle requests from users and provide results based on these requests.

261 Questions

How do you retrieve table data from mysql to jsp?

<%@ page import ="java.sql.*" %>

<%@ page import ="javax.sql.*" %>

<%@ page import ="java.io.*" %>

<%@ page language="java" import="java.util.*,java.text.SimpleDateFormat;" pageEncoding="ISO-8859-1"%>

<html>

<%

Calendar currentDate = Calendar.getInstance();

SimpleDateFormat formatter= new SimpleDateFormat("dd/MM/yyyy");

String dateNow = formatter.format(currentDate.getTime());

String nam=(String)request.getAttribute("uname");

String pwd=(String)request.getAttribute("pwd");

request.setAttribute("date",dateNow);

request.setAttribute("nam",nam);

request.setAttribute("pwd",pwd);

System.out.print(nam);

%>

<%

String full_name=request.getParameter("fname");

String cn=request.getParameter("name");

String deg=request.getParameter("designation");

String ema=request.getParameter("email");

String mobile=request.getParameter("mob_no");

String serial=request.getParameter("ser_no");

String department=request.getParameter("deptt_name");

String off_add=request.getParameter("office_address");

String st_name=request.getParameter("state_name");

String spin=request.getParameter("pin");

String phn_no=request.getParameter("phone_no");

String svar1=request.getParameter("srvip");

String svar2=request.getParameter("srvloc");

String svar3=request.getParameter("destport");

String svar4=request.getParameter("descrsrv");

String placen=request.getParameter("place");

String vdate=request.getParameter("date");

String rname=request.getParameter("r_name");

String rdesg=request.getParameter("r_designation");

String remail=request.getParameter("r_email");

String rphone=request.getParameter("r_phone");

String cname=request.getParameter("c_name");

String cdesg=request.getParameter("c_designation");

String cemail=request.getParameter("c_email");

String cphone=request.getParameter("c_phone");

String issby=request.getParameter("captchatext");

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/vpn","root","test123");

Statement st= con.createStatement();

int i=st.executeUpdate("insert into vpn_registration(cname,name,designation,email,mobile,serial_no,name_dept,off_add,state,pincode,off_phone,place,rname,rdesign,rmail,rmob,c_name,cdesign,cmail,cmob,issuedby) values ('"+cn+"','"+full_name+"','"+deg+"','"+ema+"','"+mobile+"','"+serial+"','"+department+"','"+off_add+"','"+st_name+"','"+spin+"','"+phn_no+"','"+placen+"','"+rname+"','"+rdesg+"','"+remail+"','"+rphone+"','"+cname+"','"+cdesg+"','"+cemail+"','"+cphone+"','"+issby+"')");

Statement st1= con.createStatement();

int y=st1.executeUpdate("insert into server_details(registration_no,serip,serloc,destport,desc_service) values ('"+svar1+"','"+svar2+"','"+svar3+"','"+svar4+"')");

st.close();

con.close();

%>

<head>

<title>ONLINE VPN REGISTRATION FORM</title>

<style>

a, A:link, a:visited, a:active

{color: #0000aa; text-decoration: none; font-family: Tahoma, Verdana; font-size: 11px}

A:hover

{color: #ff0000; text-decoration: none; font-family: Tahoma, Verdana; font-size: 11px}

p, tr, td, ul, li

{color: #000000; font-family: Tahoma, Verdana; font-size: 11px}

.header1, h1

{color: #ffffff; background: #4682B4; font-weight: bold; font-family: Tahoma, Verdana; font-size: 13px; margin: 0px; padding-left: 2px; height: 21px}

.ctrl

{font-family: Tahoma, Verdana, sans-serif; font-size: 12px; width: 100%;}

.btnform

{border: 0px; font-family: tahoma, verdana; font-size: 12px; background-color: #DBEAF5; width: 100%; height:18px; text-align: center; cursor: hand;}

.btn

{background-color: #DBEAF5; padding: 0px;}

textarea, select,input

{font: 9px Verdana, arial, helvetica, sans-serif; background-color: #DBEAF5;}

</style>

<SCRIPT language="javascript">

function addRow(tableID) {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;

if(rowCount>6)

{

alert("Cannot add more than 6 rows.");

return false;

}

document.getElementById("rowlength").value=rowCount;

var row = table.insertRow(rowCount);

var counts=rowCount-1;

var cell0 = row.insertCell(0);

var chk = document.createElement("input");

chk.type = "checkbox";

chk.name="chk"+counts+"";

cell0.appendChild(chk);

var cell1 = row.insertCell(1);

var srvip = document.createElement("input");

srvip.name="srvip"+counts+"";

srvip.id="srvip"+counts+"";

srvip.className='ctrl';

srvip.type = "text";

//srvip.onClick="hello()";

cell1.appendChild(srvip);

var cell2 = row.insertCell(2);

var srvloc = document.createElement("input");

srvloc.type = "text";

srvloc.className='ctrl';

srvloc.name="srvloc"+counts+"";

cell2.appendChild(srvloc);

var cell3 = row.insertCell(3);

var destport = document.createElement("input");

destport.type = "text";

destport.className='ctrl';

destport.name="destport"+counts+"";

destport.id="destport"+counts+"";

cell3.appendChild(destport);

var cell4 = row.insertCell(4);

var descrsrv = document.createElement("input");

descrsrv.type = "text";

descrsrv.className='ctrl';

descrsrv.name="descrsrv"+counts+"";

cell4.appendChild(descrsrv);

}

</SCRIPT>

<script type="text/javascript">

function clearFields()

{

document.cmaForm.reset();

}

</script>

<SCRIPT language="javascript">

function deleteRow(tableID) {

try {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;

for(var i=0; i<rowCount; i++) {

var row = table.rows[i];

var chkbox = row.cells[0].childNodes[0];

if(null != chkbox && true == chkbox.checked) {

if(rowCount <= 2) {

alert("Cannot delete all the rows.");

break;

}

table.deleteRow(i);

rowCount--;

i--;

}

}

}catch(e) {

alert(e);

}

}

</SCRIPT>

<script type="text/javascript">

function setid()

{

var x=document.cmaForm.rowlength.value;

//alert(x);

document.cmaForm.id=x;

return true;

}

</script>

<script type="text/javascript" src="vpn.js"></script>

</head>

<body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="noBack();" bgcolor="white">

<!-- Header -->

<table cellpadding="0" cellspacing="0" width="100%" border="0" bgcolor="#ffffff">

<tr>

<td align="center"><img src="img/nic.jpg" width="65%" height="80" border="0" alt="NIC Logo"></td>

</tr>

<tr><td><img src="img/line9.gif" width="1" height="5" border="0"></td></tr>

</table>

<!-- Form -->

<form method="post" name="cmaForm" action="vpn_registration.jsp" id="0" onSubmit="return setid(),validate_form(this.id)" >

<table cellpadding="0" cellspacing="0" border="0" width="65%" align="center">

<tr>

<td bgcolor="#4682B4" width="10"><img src="img/line9.gif" width="10" height="1" border="0"></td>

<td class="header1" nowrap>NIC VPN Services<img src="img/line9.gif" width="10" height="1" border="0"></td>

<td><img src="img/line5.gif" width="10" height="21" border="0"></td>

<td background="img/line8.gif" width="100%">&nbsp;</td>

<td background="img/line8.gif"><img src="img/line9.gif" width="10" height="1" border="0"></td>

</tr>

<tr>

<td background="img/line6.gif"><img src="img/line9.gif" border="0"></td>

<td colspan="3"><img src="img/line9.gif" width="1" height="10" border="0"><br>

<table cellpadding="0" cellspacing="0" border="0" width="100%">

<tr><td bgcolor="#DBEAF5">

<table cellspacing="1" cellpadding="2" border="0" width="100%">

<tr bgcolor="#ffffff">

<td colspan="7" align="center"><font size="2">National Infomatics Centre <br> Department of Information Technology<br>Ministry of Communications and Information Technology

<br> Government of India</font></td>

</tr>

<tr bgcolor="#4682B4">

<td colspan="7" align="center"><font size="2" color="white"><b>VPN Registration Form</b></font></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7"><b>Note:</b>&nbsp;&nbsp;Fields marked<font size="2" color="red"> * </font>are mandatory.

</td>

</tr>

<logic:present name="fail">

<tr><td colspan=7 align="center" ><font color="red" size="5">${fail}</font></td></tr>

</logic:present>

<tr bgcolor="#ffffff">

<td colspan="7" align="center"><b>SECTION I : SUBSCRIBER INFORMATION</b></td>

</tr>

<tr bgcolor="#CCCCCC">

<td colspan="7">&nbsp;1.1 Personal Details:</td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;Full Name:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="fname" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;Common Name:</td>

<td colspan="5" width="50%"><input type="text" name="name" value="<%= request.getParameter("uname")%>" readonly="readonly"class="ctrl"></td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;Designation:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="designation" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;E-mail Address:<font size="2" color="red">* </font></td>

<td colspan="5" width="50%"><input type="text" name="email" class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;Mobile:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="mob_no" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;DC Serial No.:</td>

<td colspan="5" width="50%"><input type="text" name="ser_no" value="<%= request.getParameter("pwd")%>" readonly="readonly"class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#CCCCCC">

<td colspan="7">&nbsp;1.2 Official Address:</td>

</tr>

<tr bgcolor="#ffffff">

<td>&nbsp;Name of the Department:<font size="2" color="red">* </font></td>

<td bgcolor="#ffffff" colspan="7"><input type="text" name="deptt_name" size="35" class="ctrl"></td>

</tr>

<tr bgcolor="#ffffff">

<td >&nbsp;Office Address:<font size="2" color="red">* </font></td>

<td colspan="7"><input type="text" name="office_address" size="35" class="ctrl"></td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;State:</td>

<td colspan="1" width="35%"><input type="text" name="state_name" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;Pin Code:<font size="2" color="red">* </font></td>

<td colspan="5" width="50%"><input type="text" name="pin" class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%" >&nbsp;Office Telephone:</td>

<td colspan="1" width="35%"><input type="text" name="phone_no" size="35" class="ctrl"></td>

<td colspan="6" width="5%"></td>

</tr>

<tr bgcolor="#CCCCCC">

<td colspan="7">&nbsp;1.3 List of Server to access through VPN:<font size="2" color="red">* </font></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7">

<TABLE border="1" class="ctrl" id="addressesTable">

<TR>

<td width="21">&nbsp;</td>

<TD width="150">IP Address of the Server</TD>

<TD width="196">Location of the Server</TD>

<TD width="167">Destination Port</TD>

<TD width="162">Website/URL of Servers</TD>

</TR>

<TR>

<TD>

<INPUT name="chk" type="checkbox" class="ctrl" disabled="disabled"/></TD>

<TD><INPUT name="srvip0" type="text" id="srvip0" class="ctrl" /></TD>

<TD><input name="srvloc0" type="text" class="ctrl" value=""></TD>

<TD><INPUT name="destport0" type="text" id="destport0"class="ctrl" /></TD>

<TD><INPUT name="descrsrv0" type="text" class="ctrl" />

<input type="hidden" name="rowlength" id="rowlength" value="1">

</TD>

</TR>

</TABLE>

</td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7" align="left"><input name="des_ser2" value="Add New" type="button" onClick="addRow('addressesTable')" />

<INPUT type="button" value="Delete" onClick="deleteRow('addressesTable')" /></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7" align="center"><font size="2"><u>DECLARATION</u></font></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7">I hereby declare that<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1. The information provided is correct.<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2. Is responsible for the safety of the Digital Certificate, PIN, Username and Password issued for accessing VPN Service.<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3. I undertake to surrender the VPN account and Digital certificate on transfer / leaving the division.<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4. The certificate and VPN account are issued will be used only for accessing the NIC VPN Service as per the list provided in 1.3.<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5. Will not indulge in any activity and no attempt will be made to gain unauthorized access to other NIC Websites and facilities.<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6. I am responsible for the content/ data uploaded in the servers through VPN connection.<br>

&nbsp;I have read the terms and conditions of NIC VPN Services and will comply with. If at a later stage any information is found to be incorrect or non-compliance with the terms and conditions will result in the cancellation of the DC issued by NIC for NIC VPN service.</td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;Place:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="place" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;Date:</td>

<td colspan="5" width="50%"><input type="text" name="date" value="${date}" readonly="readonly" class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7" align="center"><b>SECTION II : RECOMMENDATION</b></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7">&nbsp;This is to certify that the person as identified in SECTION-I has provided correct information and is authorized on behalf of the organization to update and access servers listed in 1.3.<br>

&nbsp;I shall intimate NIC VPN Division to deactivate the account when the person is transferred / relived from responsibility for which the VPN account and digital certificate is issued.

</td>

</tr>

<tr bgcolor="#CCCCCC">

<td colspan="7">&nbsp;2.1 Verification by Recommending/Reporting/Head of Office</td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;Name:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="r_name" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;Designation:<font size="2" color="red">* </font></td>

<td colspan="5" width="50%"><input type="text" name="r_designation" class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;E-mail Address:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="r_email" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;Contact No.:<font size="2" color="red">* </font></td>

<td colspan="5" width="50%"><input type="text" name="r_phone" class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7" align="center"><b>SECTION III : Verification by NIC-Coordinator</b></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7" align="left" ><a href="http://webservices.nic.in/webcoord.aspx" target="blank"> <u> &nbsp;Please refer the authorised NIC Web Co-ordinator</a></u></td>

</tr>

<tr bgcolor="#ffffff">

<td colspan="7">&nbsp;User Category: Free / Paid<br>

&nbsp;If free enclose the copy of the approval.<br>

&nbsp;If paid, then confirm the period for which payment is made: From:.................To:.................<br>

&nbsp;Project No. ......................................................................................................................<br>

&nbsp;The web sites mentioned at 1.3 (a) in SECTION-I am correct. The subscriber is the authorized person to update this web site and require VPN Services. Permission may give for the same.<br>

&nbsp;I shall intimate NIC VPN division to deactivate the account when the person is transferred / relived from responsibility for which the VPN account and digital certificate is issued.

</td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;Name:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="c_name" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;Designation:<font size="2" color="red">* </font></td>

<td colspan="5" width="50%"><input type="text" name="c_designation" class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;E-mail Address:<font size="2" color="red">* </font></td>

<td colspan="1" width="35%"><input type="text" name="c_email" size="12" class="ctrl"></td>

<td colspan="1" width="5%">&nbsp;Contact No.:<font size="2" color="red">* </font></td>

<td colspan="5" width="50%"><input type="text" name="c_phone" class="ctrl" size="10"></td>

</tr>

<tr bgcolor="#ffffff">

<td width="15%">&nbsp;Enter the Letters:<font size="2" color="red">* </font></td>

<td colspan="1" width="20%"><input type="text" name="captchatext" size="12" class="ctrl"></td>

<td colspan="1" width="20%">&quot;</td>

<td colspan="1" width="20%"><input type="image" name="captchaimg" src="/vpn_app/jcaptcha" size="12" class="ctrl"></td>

<td colspan="4" width="25%"></td>

</tr>

</table></td></tr></table>

<img src="img/line9.gif" width="1" height="10" border="0"><br>

</td>

<td background="img/line7.gif"><img src="img/line9.gif" border="0"></td>

</tr>

<tr>

<td width="10"><img src="img/line4.gif" width="10" height="20" border="0"></td>

<td bgcolor="#4682B4" colspan="4" align="right">

<table cellpadding="0" cellspacing="0" border="0">

<tr>

<td class="btn" width="100"><input type="submit" name="Submit" value="Submit" class="btnform"></td>

<td width="1"><img src="img/line9.gif" width="1" height="18" border="0"></td>

<td class="btn" width="100"><input type="reset" name="Reset" value="Reset" class="btnform"></td>

<td width="1"><img src="img/line9.gif" width="1" height="18" border="0"></td>

</tr>

</table>

</td>

</tr>

</table>

</form>

<!-- Footer -->

<table align="center" cellpadding="3" cellspacing="0" width="65%" border="0" height="22">

<tr bgcolor="#DBEAF5">

<td align="center"><font face="Tahoma">For any query please contact VPN Support:</font><font color="blue" face="Tahoma">vpnsupport[at]nic[dot]in</font>

<br>

<font face="Tahoma">Contact No:</font> <font color="blue" face="Tahoma">+91-11-24305391 / 99</font></td>

</tr>

<logic:present name="fail">

${fail}

</logic:present>

</table>

<!-- /Footer -->

</body>

</html>

How do you get the image file from the database with JSP?

You can retrieve an image file from a database in JSP by writing a servlet that fetches the image from the database and streams it to the JSP page. The servlet will set the content type to "image/jpeg" or the appropriate image format and write the image data to the response output stream. In the JSP page, you can then display the image by setting the source attribute of the img tag to the servlet URL.

Is the JSP tutorial easy to follow and understand?

Yes, the JSP tutorial is easy to follow and understand. It tells you what it is doing from start to finish. When the JSP life cycle is complete a screen will pop up and they are finished.

What is javax servlet?

A Servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web servers

A Servlet is a Java-based server-side web technology.

The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.

How many page directive can use in a single JSP?

There is no limit as such. You can use one or more Page Directives in a JSP but you cannot have duplicates

Why you use java server pages?

it's provide dynamically facility to web pages...

How you can display servlet page in jsp page?

You cannot. You display JSP Pages using the help of Servlets. Servlets are of the background classes and you cannot display them

Which method must to override in Servlet?

There are 3 main types of requests that get processed by a Servlet. They are:

• Get

• Post

• Put

Each of them have a corresponding doXXX() method in the Servlet class which would be:

• doGet

• doPost

• doPut

You must override either of these methods based on the type of requests that would be processed by your servlet

Is Servlet instance Thread safe If not how can you make thread safe?

No. The Servlet is not thread-safe by default. You can make it thread safe by implementing the SingleThreadedModel interface

What is the difference between applet and jsp?

Applet is an executable file that is automatically downloaded when we open a browser. Swing applications can operate on local data when the network is not there.

Generic Servlet and HTTP Servlet?

javax.servlet.GenericServlet

Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable

  • GenericServlet defines a generic, protocol-independent servlet.
  • GenericServlet gives a blueprint and makes writing servlet easier.
  • GenericServlet provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface.
  • GenericServlet implements the log method, declared in the ServletContext interface.
  • To write a generic servlet, it is sufficient to override the abstract service method.

javax.servlet.http.HttpServlet

Signature: public abstract class HttpServlet extends GenericServlet implements java.io.Serializable

  • HttpServlet defines a HTTP protocol specific servlet.
  • HttpServlet gives a blueprint for Http servlet and makes writing them easier.
  • HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.

How can a Servlet call a JSP error page?

Declare That a JSP Page Is an Error Page

The isErrorPage attribute tells the container if the current page is available to be an error page for another JSP page (the current page is the URL in another page's errorPage attribute). Ex:

<%@ page isErrorPage="true" %>

If this attribute is set to "true" then the variable "exception" is available to you. Otherwise (default is "false"), if you try to reference the exception, you will get a fatal error.

So, when a Servlet request being forwarded to a JSP Page faces an exception/error this error page would get called.

How do you run servlet in tomcat?

You Run a Servlet by deploying it on the server, which in this case is tomcat. We need to deploy the servlet in the Web Applications context.

When we talk of a Web application's context, we are referring to that Web application's root directory or path within a particular server. A special directory exists within the application hierarchy named WEB-INF. This directory contains all things related to the application that aren't in the document root of the application. It is this WEB-INF directory that is the Web application's root directory, also referred to as the context. The most important file here is web.xml, the name of the WebApp deployment descriptor.

Let us take a look at the various contents (files & folders) that will be present inside this WEB-INF directory:

1. /WEB-INF/web.xml - This is the Deployment Descriptor file

2. /WEB-INF/classes/ - This is the directory where all your java class files will be placed

3. /WEB-INF/lib/ - This is the folder where all your JAR files go.

my tomcat is installed in c:\dev\java\Jakarta-tomcat-4.0.1. This is my TOMCAT_HOME. Remember that, this path that is referred by TOMCAT_HOME might vary from PC to PC depending on the installation preferences of the user.

To Deploy the Servlet, you need to place the .class file corresponding to your servlet in the /WEB-INF/classes/ folder

Can variable declared in one scriplet access in another scriplet?

No. Variables declared inside a scriptlet are like method local variables which are not accessible outside the scriptlet/method.

Is jsp is platform independents?

Yes. JSP is a java based technology and hence it is platform independent. So, you can run it in any environment. The same JSP that can run in a windows box can always run in a Linux box.

What is generic servlet class?

GenericServlet defines a generic, protocol-independent servlet whereas HttpServlet Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site that uses the Http Protocol.

What is the purpose of page directive in JSP?

The Page Directive is one of the important components of any JSP Page. It can help us define page specific properties like Buffer size or location of an error page etc

A JSP page, and any files included via the include directive, can contain one or more page directives but no duplicates. The JSP container will apply all the attributes to the page. The position of these page directives is irrelevant, but it is good practice to keep them together at the top of the page. (So that we can identify them easily)

What does servlet session API means?

The Servlet Session API refers to the documentation on the list of methods available in the HttpSession class that is used for Session Management

The HttpSession object is very powerful and has many features. Before we dig deep into the world of Sessions, let us take a look at the important methods they have so that we can use them effectively.

• getAttribute(java.lang.String name) - Returns an Object associated with that name that was stored in the session object.

• getAttributeNames() - Returns an Enumeration object that lists the names of the objects stored in the session object.

• getCreationTime() - Returns a long containing the date stamp of creation.

• getId() - Returns the id of the session as a String representing the unique identifier assigned to this session.

• getLastAccessedTime() - Returns a long containing the date stamp of the last time the session was accessed.

• getMaxInactiveInterval() - Returns an integer representing the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.

• invalidate() - This destroys a session. It can't be referenced after this method has been called.

• isNew() - This tells you whether the client knows about the session. In other words, the has session been created by the server, but the client hasn't acknowledged receiving the session ID yet.

• removeAttribute(java.lang.String name) - This removes an attribute. It deletes it from the session object.

• setAttribute(java.lang.String name, java.lang.Object value) - You use this method to add objects to a session.

• setMaxInactiveInterval(int interval) - Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

• getValue(java.lang.String name) - Returns an Object associated with that name that was stored in the session object. As of Version 2.2, this method is replaced by getAttribute(). The getAttribute() method will be on the exam, not this one, but it is here for completeness.

• getValueNames() - Returns a String array with a list of names associated with the objects added to a given session. As of Version 2.2, this method is replaced by getAttributeNames(). The getAttributeNames() method will be on the exam, not this one, but it is here for completeness.

• putValue(java.lang.String name, java.lang.Object value) - You use this method to add objects to a session. This has been deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object). The setAttribute() method will be on the exam, not this one, but it is here for completeness.

• removeValue(java.lang.String name) - This removes a value, but retains an attribute name in the session. The name is valid but the object is null. This has been deprecated. As of Version 2.2, this method is replaced by removeAttribute(). The removeAttribute() method will be on the exam, not this one, but it is here for completeness.

How do you compile servlet in java using tomcat?

Tomcat is a server. It is used to deploy and run Servlets and not compile them. A Servlet is a java file and has to be compiled just like any other Java Class.

What are jsp actions?

JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine. JSP Actions consist of a typical (XML-based) prefix of "jsp" followed by a colon, followed by the action name followed by one or more attribute parameters.

There are six JSP Actions:

< jsp : include / >

< jsp : forward / >

< jsp : plugin / >

< jsp : usebean / >

< jsp : setProperty / >

< jsp : getProperty / >

What is servlet API in java?

Overview of the Servlet API

The Servlet 2.3 API consists of two packages: javax.servlet and javax.servlet.http. The base functionality is defined in the javax.servlet package whose classes and interfaces outline a generic, protocol-independent implementation. This means you can use it for non-Web applications, too. Of course, the exam targets the Web, so the HTTP protocol is the only one discussed in this blog. The javax.servlet.http interface defines classes and interfaces that are specific to the Hypertext Transfer Protocol (HTTP).

The javax.servlet Package

The javax.servlet package defines 12 interfaces, 7 classes, and 2 exceptions. These interfaces, classes, and exceptions are as follows.

Interfaces

• Filter:- Preprocessor of the request before it reaches a servlet. It can also be a postprocessor of the response leaving a servlet. It can modify a request or response (for example, change headers), the request to a resource (a servlet or static content), or the response from a resource.

• RequestDispatcher:- This is the servlet version of a redirect. It enables requests to be processed and then forwarded to other components of a Web application, such as another servlet, HTML file, or JSP file.

• Servlet:- Defines the life-cycle methods that are implemented by all servlets.

• ServletConfig:- This class has the methods for accessing the servlet configuration information such as the servlets name (from the web.xml file), the initialization parameters, and the ServletContext object.

• ServletContext:- These methods enable your servlet to communicate with its servlet container. This is how you get the MIME type of a file, dispatch requests, or write to a log file. Notice that this information has application scope. The most important features of the ServletContext are application-scope attributes access, logging, and context initialization parameters.

• ServletContextAttributeListener:- Implementations of this interface receive notifications of changes to the attribute list on the servlet context of a Web application. Supports the handling of the ServletContextAttributeEvent class.

• ServletContextListener:- An interface that supports the handling of the ServletContextEvent class. Defines a set of methods that a servlet uses to communicate with its servlet container. It can get the MIME type of a file, dispatch requests, or write to a log file. Notice that there is one context per "Web application" per Java Virtual Machine. The specification defines a "Web application" as a collection of servlets and content installed under a specific subset of the server's URL namespace, such as /catalog, and possibly installed via a .war file.

• ServletRequest:- This interface forms the base for the class that provides client request information to a servlet. It is protocol-independent.

• ServletResponse:- This interface forms the base for the class that represents the response sent from the servlet to the client.

• SingleThreadModel:- An interface that ensures a given servlet handles only one request at a time.

Classes

• GenericServlet:- An abstract class that implements ServletConfig. It defines a generic, protocol-independent servlet.

• ServletContextAttributeEvent:- This is the event class for notifications about changes to the attributes of the servlet context of a Web application.

• ServletContextEvent:- This is the event class for notifications about changes to the servlet context of a Web application (parent of ServletContextAttributeEvent).

• ServletInputStream:- Provides an input stream for reading binary data from a client request. You can modify it; it already has the readLine method for reading data one line at a time.

• ServletOutputStream:- An abstract class providing an output stream for sending binary data to the client. You print HTML, XML, or other output to the client using ServletOutputStream's print() and println() methods.

• ServletRequestWrapper:- Provides a convenient implementation of the ServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This is where you can modify the behavior of request objects.

• ServletResponseWrapper:- Provides a convenient implementation of the ServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet. This is where you can modify the behavior of response objects.

Exceptions

• ServletException:- Extends java.lang.Exception to provide a base class for defining servlet-related extensions.

• UnavailableException:- Extends ServletException to indicate that a servlet is temporarily or permanently unavailable.

The javax.servlet.http Package

The javax.servlet.http package defines eight interfaces and seven classes. These interfaces and classes are as follows:

Interfaces

The interfaces include the following:

• HttpServletRequest:- Extends javax.servlet.ServletRequest to support HTTP.

• HttpServletResponse:- Extends javax.servlet.ServletResponse to support HTTP.

• HttpSession:- Defines methods that provide access to persistent session-state information.

• HttpSessionActivationListener:- Implemented to handle the HttpSessionActivationEvent.

• HttpSessionAttributeListener:- Implemented to handle the HttpSessionAttributeEvent.

• HttpSessionBindingListener:- Implemented by objects that listen for HttpSessionBindingEvent events.

• HttpSessionListener:- Implemented to handle the HttpSessionEvent.

Classes

The classes include the following:

• Cookie:- Encapsulates HTTP cookies.

• HttpServlet:- An abstract class that extends javax.servlet.GenericServlet to provide support for HTTP.

• HttpServletRequestWrapper:- Provides the capability to wrap and modify incoming HttpServletRequest objects.

• HttpServletResponseWrapper:- Provides the capability to wrap and modify outgoing HttpServletResponse objects.

• HttpSessionBindingEvent:- Extends java.util.EventObject to define an event that is sent to an HttpSessionBindingListener when an object is bound or unbound from the current HttpSession.

• HttpSessionEvent:- Parent class of HttpSessionBindingEvent.

Is jsp thread safe by default?

No. JSPs are not thread safe by default

What does template contain in web application?

It contains instruction examples, text, and checkpoint standards for individual phase in the activity.

You must be able to review and update the output document created from the template at regular intervals throughout the application life cycle. After developing the application design, you can easily add more details step by step in the output document.