Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
119 views6 pages

Java Servlet and Session Guide

The document provides code examples and explanations of servlet concepts including the servlet context and servlet config, session tracking, URL patterns, generating HTML content from servlets, and examples of JSP and JSTL. It includes code for a servlet that retrieves initialization parameters from the servlet config, code demonstrating session management with servlets, and a servlet that generates a simple HTML form directly instead of using a JSP.

Uploaded by

sulohita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views6 pages

Java Servlet and Session Guide

The document provides code examples and explanations of servlet concepts including the servlet context and servlet config, session tracking, URL patterns, generating HTML content from servlets, and examples of JSP and JSTL. It includes code for a servlet that retrieves initialization parameters from the servlet config, code demonstrating session management with servlets, and a servlet that generates a simple HTML form directly instead of using a JSP.

Uploaded by

sulohita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

[ Servlet Context and ServletConfig ]

http://www.java-forums.org/ocpjwcd/54105-tutorial-review-web-container-model-services-web-componentdeveloper-exam.html

http://www.beingjavaguys.com/2013/05/difference-between-request-dispatcher.html

http://www.studytonight.com/servlet/servlet-config.php

Wrap our sleeves and do some coding.


Creating the Servlet.
package servletspack;
import java.io.IOException;
import java.io.PrintWriter;
import
import
import
import
import

javax.servlet.ServletConfig;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;

public class InitializationParamatersUsingServletConfig extends HttpServlet {


protected void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
ServletConfig conf=getServletConfig();
String s1=conf.getInitParameter("companyName");
String s2=conf.getInitParameter("companyEmail");
pw.println("You work for " +s1+ " and your email id is " +s2);
pw.close();
}
}
<web-app>

<servlet>
<display-name>InitializationParamatersUsingServletConfig</display-name>
<servlet-name>InitializationParamatersUsingServletConfig</servlet-name>
<servlet-class>servletspack.InitializationParamatersUsingServletConfig</servlet-class>
<init-param>
<param-name>companyName</param-name>
<param-value> JumboTheBumbo</param-value>
</init-param>
<init-param>
<param-name>companyEmail</param-name>
<param-value> [email protected]</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitializationParamatersUsingServletConfig</servlet-name>
<url-pattern>/InitParamServletConfig</url-pattern>
</servlet-mapping>
</web-app>
http://www.java4s.com/java-servlet-tutorials/example-of-servletconfig-in-java-servlet/
web.xml Deployment Descriptor Elements
http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html
[Session Tracking]

http://www.developersbook.com/servlets/interview-questions/servlets-interview-questions-faqs.php
http://www.developersbook.com/servlets/interview-questions/servlets-interview-questions-faqs-2.php
http://www.developersbook.com/servlets/interview-questions/servlets-interview-questions-faqs-3.php
http://www.studytonight.com/servlet/session-management.php
http://www.c-sharpcorner.com/UploadFile/0d4935/session-management-using-url-rewritting-instead-of-cookies/
http://www.c-sharpcorner.com/UploadFile/73d82f/session-management-and-cookies-in-java/
http://www.c-sharpcorner.com/UploadFile/0d4935/working-with-cookies/

[URL Patterns]
http://www.roguewave.com/portals/0/products/hydraexpress/docs/3.5.0/html/rwsfservletug/4-3.html
http://www.c-sharpcorner.com/uploadfile/satyapriyanayak/cookies-servlet-in-java/
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index Page</title>
</head>
<body>
<form method="POST" action="Validate">
User:
<input type="text" name="user" />
<br /> Password:
<input type="text" name="pass" />
<br />
<input type="submit" value="submit" />
</form>
</body>
</html>
Validate.java
import
import
import
import
import
import

java.io.IOException;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
javax.servlet.http.HttpSession;

public class Validate extends HttpServlet {


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String usr=request.getParameter("user");
String pas=request.getParameter("pass");
if(pas.equals("12345"))

}
else
{
}

//Crating a session
HttpSession s=request.getSession();
s.setAttribute("userName", usr);
response.sendRedirect("/SessionManagement/Welcome");

response.getWriter().println("Invalid Credentials!");

}
}
Welcome.java
import
import
import
import
import
import
import

javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
javax.servlet.http.HttpSession;
java.io.PrintWriter;
java.io.IOException;

public class Welcome extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
String user=(String)session.getAttribute("userName");
out.println("Hello " + user);
}
}

Web.xml
========
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SessionManagement</display-name>
<servlet>
<description></description>
<display-name>Validate</display-name>
<servlet-name>Validate</servlet-name>
<servlet-class>Validate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Validate</servlet-name>
<url-pattern>/Validate</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Welcome</display-name>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
</web-app>
[Generating HMTL content inside a Servlet]

import
import
import
import
import
import
import

javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
javax.servlet.http.HttpSession;
java.io.PrintWriter;
java.io.IOException;

public class IndexServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<html><head><title>Index Page</title></head><body><form method='POST'
action='Validate'>
User:
<input type='text' name='user' /> <br /> Password: <input type='text'
name='pass' /> <br /> <input type='submit' value='submit' />
</form></body></html>");
}
}
[JSP Examples]

http://www.codemiles.com/jsp-examples/

[JSTL Examples]
http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm
http://www.journaldev.com/2064/jsp-expression-language-el-example-tutorial
http://www.javatpoint.com/jstl#core

You might also like