Wt_Shorts-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

‭UNIT-IV SHORT QUESTIONS [2M]‬

‭1. How is Servlet different from an Applet?‬

‭●‬ ‭Servlet‬‭:‬
‭●‬ ‭Runs on the server side.‬
‭●‬ ‭Generates dynamic web content.‬
‭●‬ ‭Does not have a graphical user interface.‬
‭●‬ ‭Requires a web server to run.‬
javax.servlet‬‭package.‬
‭●‬ ‭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.applet‬‭package.‬
‭●‬ ‭Written in Java and uses the‬‭

‭2. Explain about Servlet API.‬

‭●‬ ‭The‬‭Servlet API‬‭provides a standard interface for‬‭building web applications in Java.‬


‭●‬ ‭Key interfaces include:‬
‭●‬ ‭
Servlet‬
‭: Defines methods that all servlets must implement.‬
‭●‬ ‭
ServletRequest‬
‭: Provides client request information‬‭to a servlet.‬
‭●‬ ‭
ServletResponse‬
‭: Provides a response object to assist‬‭a servlet in sending a‬
‭response to the client.‬
‭●‬ ‭
HttpServletRequest‬ ServletRequest‬‭to provide‬‭request‬
‭: Extends‬‭
‭information for HTTP servlets.‬
‭●‬ ‭
HttpServletResponse‬ ServletResponse‬‭to provide‬
‭: Extends‬‭
‭HTTP-specific functionality in sending a response.‬

‭3. How to create a cookie using servlet?‬

i‭mport 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!");‬
‭}‬
‭}‬

‭4. What is servlet?‬

‭●‬ ‭A‬‭Servlet‬‭is a Java programming language class used‬‭to 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.‬

‭5. What are the Differences between GenericServlet and HttpServlet?‬

‭●‬ ‭GenericServlet‬‭:‬
Servlet‬‭interface.‬
‭●‬ ‭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‬

‭●‬ ‭An‬‭HTTP Request‬‭is a message sent by the client to‬‭a 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 the‬‭request (e.g.,‬‭ ‭,‬
User-Agent‬
‭ Content-Type‬
‭,‬‭ ‭).‬
‭●‬ ‭Body‬‭: Contains data sent to the server (used in POST,‬‭PUT requests).‬

‭7. What is Session?‬

‭●‬ ‭A‬‭Session‬‭is a way to store information about a user‬‭across 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.‬

‭8. Discuss about Http Responses‬

‭●‬ ‭An‬‭HTTP Response‬‭is a message sent by the server to‬‭the 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 the‬‭response (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-side‬‭and sent with every HTTP‬
‭request.‬
‭●‬ ‭URL Rewriting‬‭: Appends the session ID to the URL.‬
‭●‬ ‭Hidden Form Fields‬‭: Stores session information in‬‭form fields that are hidden from‬
‭the user.‬
‭●‬ ‭SSL Sessions‬‭: Uses Secure Socket Layer to maintain session information.‬

‭10. Explain about Common Gateway Interface (CGI).‬

‭●‬ ‭Common Gateway Interface (CGI)‬‭is a standard for external‬‭gateway 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.‬
‭●‬

‭UNIT-V SHORT QUESTIONS [2M]‬

‭1. Define JSP Expression.‬

‭●‬ ‭JSP Expression‬‭: A JSP expression is used to output‬‭the value of a Java expression‬
<%=‬
‭directly into the client's response. The syntax for a JSP expression is‬‭
expression %>‬
‭ ‭.‬
‭Example‬‭:‬

‭<%= "Hello, World!" %>‬


‭<%= new java.util.Date() %>‬

‭2. What are the directives in JSP with syntax?‬

‭●‬ ‭Directives‬‭in JSP provide global information about‬‭the 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" %>‬

‭●‬

I‭ nclude Directive‬‭: Includes a file during the translation‬‭phase.‬


‭<%@ include file="relativeURL" %>‬
‭Example‬‭:‬
‭<%@ include file="header.jsp" %>‬

‭●‬
jsp <%@‬
‭●‬ ‭Taglib Directive‬‭: Declares a tag library, defining‬‭custom tags.‬‭
taglib uri="uri" prefix="prefix" %>‬‭Example‬‭:‬‭
‭ jsp <%@‬
taglib uri="http://java.sun.com/jsp/jstl/core"‬

prefix="c" %>‬

‭3. What is the syntax of JSP?‬

‭●‬ ‭The‬‭s yntax of JSP‬‭involves a mix of HTML and Java‬‭code. The key elements‬
‭include:‬
<%@ ... %>‬
‭●‬ ‭Directives‬‭:‬‭
<% ... %>‬
‭●‬ ‭Scriptlets‬‭:‬‭
<%= ... %>‬
‭●‬ ‭Expressions‬‭:‬‭
<%! ... %>‬
‭●‬ ‭Declarations‬‭:‬‭
‭Example‬‭:‬

‭<%@ page language="java" contentType="text/html; charset=UTF-8" %>‬


‭<html>‬
‭<head>‬
‭<title>JSP Syntax Example</title>‬
‭</head>‬
‭<body>‬
‭<h1>Hello, JSP!</h1>‬
‭<%‬
‭// Scriptlet‬
‭ tring message = "Welcome to JSP";‬
S
‭%>‬
‭<p><%= message %></p>‬
‭</body>‬
‭</html>‬

‭4. Explain about Scriptlet Tag.‬

‭●‬ ‭Scriptlet Tag‬‭: A scriptlet contains Java code that‬‭is 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>‬

‭5. What are implicit objects in JSP?‬

‭●‬ ‭Implicit Objects‬‭: JSP provides several implicit objects‬‭that 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 implicit‬‭objects.‬
‭●‬ ‭
page‬
‭: Represents the servlet instance itself.‬
‭●‬ ‭
exception‬
‭: Used for error pages to represent the Throwable‬‭object.‬
‭6. Explain about the methods of getProperty() and setProperty().‬

‭●‬ ‭getProperty() and setProperty()‬‭methods are used with‬‭JavaBeans in JSP pages.‬

s‭ etProperty‬‭: Sets a property value in a JavaBean.‬


‭<jsp:setProperty name="beanName" property="propertyName" value="value"/>‬
‭Example‬‭:‬
‭<jsp:setProperty name="user" property="name" value="John"/>‬

‭●‬
jsp‬
‭●‬ ‭getProperty‬‭: Retrieves a property value from a JavaBean.‬‭
<jsp:getProperty name="beanName"‬

property="propertyName"/>‬‭Example‬‭:‬‭
‭ jsp‬
<jsp:getProperty name="user" property="name"/>‬

‭7. What is JSP?‬

‭●‬ ‭JSP (JavaServer Pages)‬‭: A technology used to create‬‭dynamic 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.‬

‭8. What are the Beans in JSP page?‬

‭●‬ ‭Beans in JSP‬‭: JavaBeans are reusable software components‬‭that 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‬‭:‬

‭<jsp:useBean id="user" class="com.example.UserBean" scope="request"/>‬


‭<jsp:setProperty name="user" property="name" value="John"/>‬
‭<p>Name: <jsp:getProperty name="user" property="name"/></p>‬
‭9. What are the differences between JSP and HTML?‬

‭●‬ ‭JSP (JavaServer Pages)‬‭:‬


‭●‬ ‭Can include Java code and logic.‬
‭●‬ ‭Generates dynamic content.‬
‭●‬ ‭Compiled into servlets by the server.‬
‭●‬ ‭Supports integration with JavaBeans, JDBC, and other Java technologies.‬
‭●‬ ‭HTML (HyperText Markup Language)‬‭:‬
‭●‬ ‭Markup language used for creating static web pages.‬
‭●‬ ‭Contains only static content.‬
‭●‬ ‭Directly interpreted by web browsers.‬
‭●‬ ‭Does not support server-side scripting or Java code.‬

‭10. How are Cookies used for Session tracking in JSP?‬

‭●‬ ‭Cookies for Session Tracking‬‭:‬


‭●‬ ‭Cookies are small pieces of data stored on the client-side and sent with every HTTP‬
‭request.‬
‭●‬ ‭In JSP, cookies can be used to maintain a session by storing a unique session‬
‭identifier.‬
‭Example‬‭:‬

‭// Creating a cookie‬


‭Cookie sessionCookie = new Cookie("JSESSIONID", session.getId());‬
‭sessionCookie.setMaxAge(60*60); // Set expiry time to 1 hour‬
‭response.addCookie(sessionCookie);‬

/‭/ Retrieving a cookie‬


‭Cookie[] cookies = request.getCookies();‬
‭if (cookies != null) {‬
‭for (Cookie cookie : cookies) {‬
‭if ("JSESSIONID".equals(cookie.getName())) {‬
‭String sessionId = cookie.getValue();‬
‭// Use sessionId for session tracking‬
‭}‬
‭}‬
‭}‬

‭Cookies help maintain continuity across multiple requests by the same user, enabling session‬
‭tracking in web applications.‬

You might also like