Professional Documents
Culture Documents
Wt_Shorts-1
Wt_Shorts-1
Wt_Shorts-1
● Servlet:
● Runs on the server side.
● Generates dynamic web content.
● Does not have a graphical user interface.
● Requires a web server to run.
javax.servletpackage.
● Written in Java and uses the
● Applet:
● Runs on the client side within a web browser.
● Provides a graphical user interface.
● Requires a Java-enabled browser to run.
java.appletpackage.
● Written in Java and uses the
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CookieExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Create a cookie
Cookie cookie = new Cookie("username", "JohnDoe");
// Set the maximum age of the cookie to one day
cookie.setMaxAge(24 * 60 * 60);
// Add the cookie to the response
response.addCookie(cookie);
r esponse.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Cookie created successfully!");
}
}
● AServletis a Java programming language class usedto extend the capabilities of
servers that host applications accessed by means of a request-response programming
model.
● Typically, servlets are used to process or store a client's request and generate a
dynamic response.
● GenericServlet:
Servletinterface.
● Abstract class that implements the
● Protocol-independent; can be used for any protocol (not just HTTP).
service()method.
● Requires overriding the
● HttpServlet:
GenericServlet
● Extends .
● Specifically designed for HTTP protocol.
doGet()
● Provides methods like doPost()
, doPut()
, doDelete()
,and .
● Most commonly used servlet class.
6. Discuss about Http Request
● AnHTTP Requestis a message sent by the client toa server to request a resource.
● Components of an HTTP request:
● Request Line: Includes the HTTP method (GET, POST,etc.), the resource path,
and the HTTP version.
Host
● Headers: Provide additional information about therequest (e.g., ,
User-Agent
Content-Type
, ).
● Body: Contains data sent to the server (used in POST,PUT requests).
● ASessionis a way to store information about a useracross multiple HTTP requests.
● Sessions are used to maintain state between client and server.
● Each session has a unique identifier (session ID) and can store user-specific data.
● AnHTTP Responseis a message sent by the server tothe client in response to an
HTTP request.
● Components of an HTTP response:
● Status Line: Includes the HTTP version, status code,and status message.
● Headers: Provide additional information about theresponse (e.g.,
Content-Type
Set-Cookie
, ).
● Body: Contains the data being sent to the client (HTML,JSON, etc.).
9. What are the different types of session tracking mechanisms supported by
Servlets?
● Cookies: Small pieces of data stored on the client-sideand sent with every HTTP
request.
● URL Rewriting: Appends the session ID to the URL.
● Hidden Form Fields: Stores session information inform fields that are hidden from
the user.
● SSL Sessions: Uses Secure Socket Layer to maintain session information.
● Common Gateway Interface (CGI)is a standard for externalgateway programs to
interface with information servers such as HTTP servers.
● CGI scripts are written in languages like Perl, Python, or C, and are executed by the
server to generate dynamic web content.
● When a request is received, the server runs the CGI script and sends its output back
to the client as a response.
● CGI has largely been replaced by more efficient technologies like servlets, which run
within the server process and offer better performance and scalability.
●
● JSP Expression: A JSP expression is used to outputthe value of a Java expression
<%=
directly into the client's response. The syntax for a JSP expression is
expression %>
.
Example:
● Directivesin JSP provide global information aboutthe entire JSP page and control
the processing of the JSP page by the JSP engine. There are three main types of
directives:
age Directive: Defines attributes that apply to the entire JSP page.
P
<%@ page attribute="value" %>
Example:
<%@ page language="java" import="java.util.*" session="true" %>
●
●
jsp <%@
● Taglib Directive: Declares a tag library, definingcustom tags.
taglib uri="uri" prefix="prefix" %>Example:
jsp <%@
taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
● Thes yntax of JSPinvolves a mix of HTML and Javacode. The key elements
include:
<%@ ... %>
● Directives:
<% ... %>
● Scriptlets:
<%= ... %>
● Expressions:
<%! ... %>
● Declarations:
Example:
● Scriptlet Tag: A scriptlet contains Java code thatis executed each time the JSP page
<% ... %>tags.
is requested. The code inside the scriptlet is placed within
Example:
<%
String userName = request.getParameter("name");
if (userName == null) {
userName = "Guest";
}
%>
<p>Welcome, <%= userName %>!</p>
● Implicit Objects: JSP provides several implicit objectsthat are automatically
available to the developers. These include:
●
request
: Represents the HttpServletRequest object.
●
response
: Represents the HttpServletResponse object.
●
out
: Used to send output to the client.
●
session
: Represents the HttpSession object.
●
application
: Represents the ServletContext object.
●
config
: Represents the ServletConfig object.
●
pageContext
: Provides access to various other implicitobjects.
●
page
: Represents the servlet instance itself.
●
exception
: Used for error pages to represent the Throwableobject.
6. Explain about the methods of getProperty() and setProperty().
●
jsp
● getProperty: Retrieves a property value from a JavaBean.
<jsp:getProperty name="beanName"
property="propertyName"/>Example:
jsp
<jsp:getProperty name="user" property="name"/>
● JSP (JavaServer Pages): A technology used to createdynamic web content. It
allows embedding Java code in HTML pages and is a part of the Java EE platform.
JSP pages are compiled into servlets by the web server, enabling the dynamic
generation of content.
● Beans in JSP: JavaBeans are reusable software componentsthat can be
manipulated visually in a builder tool. In JSP, beans are used to encapsulate the
<jsp:useBean>tag and can have
business logic. They are instantiated using the
<jsp:setProperty>and
their properties set or retrieved using
<jsp:getProperty>tags.
Example:
Cookies help maintain continuity across multiple requests by the same user, enabling session
tracking in web applications.