0% found this document useful (0 votes)
79 views20 pages

AJ Practical File (6th Sem)

The document contains details of 10 experiments conducted on Java servlets and JSP. In the experiments, students: 1) Created simple servlets to print messages using GenericServlet and HttpServlet. 2) Ran an HTML file on Tomcat server and called a servlet from an HTML file. 3) Used annotations to call a servlet. 4) Wrote JSP programs to calculate addition, display date/time using out implicit object, get form data using request object, redirect using response object, and get configuration using config object. The experiments covered core servlet and JSP concepts like servlet lifecycle, implicit objects, annotations etc. Code snippets and output screenshots were

Uploaded by

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

AJ Practical File (6th Sem)

The document contains details of 10 experiments conducted on Java servlets and JSP. In the experiments, students: 1) Created simple servlets to print messages using GenericServlet and HttpServlet. 2) Ran an HTML file on Tomcat server and called a servlet from an HTML file. 3) Used annotations to call a servlet. 4) Wrote JSP programs to calculate addition, display date/time using out implicit object, get form data using request object, redirect using response object, and get configuration using config object. The experiments covered core servlet and JSP concepts like servlet lifecycle, implicit objects, annotations etc. Code snippets and output screenshots were

Uploaded by

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

THE TECHNOLOGICAL INTITUTE OF TEXTILE AND SCIENCE’S

BIRLA COLONY, BHIWANI – 127021

ADVANCED JAVA
LAB
SUBMITTED TO SUBMITTED BY
NAME:- NITISH BANSAL
MS. MUKTA GOEL ROLL NO:- 19CE076
UNV ROLL NO:-
INDEX
S.NO. EXPERIMENT DATE PAGE NO. TEACHER’S SIGN
EXPERIMENT – 1
AIM: - Write a Servlet Program to Print a Message from Servlet
(Using GenericServlet)

CODE: -
FirstGeneric.java:
import java.io.*;
import javax.servlet.*;
public class FirstGeneric extends GenericServlet
{
public void service(ServletRequest req, ServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
out.println("<h1>Welcome to Apache Tomcat Server</h1>");
out.println("My First Generic Servlet");
out.println("</body></html>");
}
}

web.xml:
<web-app>
<servlet>
<servlet-name>FirstGeneric</servlet-name>
<servlet-class>FirstGeneric</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstGeneric</servlet-name>
<url-pattern>/FirstGeneric</url-pattern>
</servlet-mapping>
</web-app>

1
OUTPUT

2
EXPERIMENT – 2
AIM: - Write a Servlet Program to Print a Message from Servlet
(Using HttpServlet)

CODE: -
FirstHttp.java:
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class FirstHttp extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
out.println("<h1>Welcome to Apache Tomcat Server</h1>");
out.println("My First Http Servlet");
out.println("</body></html>");
}
}

web.xml:
<web-app>
<servlet>
<servlet-name>FirstHttp</servlet-name>
<servlet-class>FirstHttp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstHttp</servlet-name>
<url-pattern>/FirstHttp</url-pattern>
</servlet-mapping>
</web-app>

3
OUTPUT

4
EXPERIMENT – 3
AIM: - Write an .html File and Run it through Apache Tomcat 9.0
Server

CODE: -
FirstHTMLFile.html:
<html>
<head>
<title>localhost:8080</title>
</head>
<body>
<h1>Welcome to Apache Tomcat Server</h1>
<p>This is my first .html file run through Apache Tomcat 9.0 Server</p>
</body>
</html>

OUTPUT

5
EXPERIMENT – 4
AIM: - Write a Program to Call Servlet using an .html File

CODE: -
HTMLFile.html:
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome to Apache Tomcat Server</h1>
<p><h3>This is a .html file use to call Servlet.</h3></p><p>To call Servlet,
<a href="FirstHttp">Click Here</a></p>
</body>
</html>

FirstHttp.java:
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class FirstHttp extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
out.println("<h1>Welcome to Apache Tomcat Server</h1>");
out.println("My First Http Servlet");
out.println("</body></html>");
}
}

6
web.xml:
<web-app>
<servlet>
<servlet-name>FirstHttp</servlet-name>
<servlet-class>FirstHttp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstHttp</servlet-name>
<url-pattern>/FirstHttp</url-pattern>
</servlet-mapping>
</web-app>

7
OUTPUT

8
EXPERIMENT – 5
AIM: - Write a Program to Call Servlet using Annotations

CODE: -
SimpleServlet.java:
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("<html><body>");
out.print("<h1>Hello Servlet</h1>");
out.print("This is Servlet call by using Annotations");
out.print("</body></html>");
}
}

9
web.xml:
<web-app>
<servlet>
<servlet-name>FirstHttp</servlet-name>
<servlet-class>FirstHttp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstHttp</servlet-name>
<url-pattern>/FirstHttp</url-pattern>
</servlet-mapping>
</web-app>

OUTPUT

10
EXPERIMENT – 6
AIM: - Write a JSP Program to Calculate Addition of Two Numbers

CODE: -
Addition.jsp:
<html>
<head>
<title>Addition of two Numbers</title>
</head>
<body>
<h1>Addition of Two Numbers</h1>
<%!int add(int a,int b)
{
int sum;
sum = a + b;
return sum;
}
%>
<p>First Number = 3<br>Second Number = 5</br></p>
<h4><%= "Addition of Two Numbers = "+add(3,5)%></h4>
</body>
</html>

OUTPUT

11
EXPERIMENT – 7
AIM: - Write a JSP Program to show the use of Out Implicit
Object

CODE: -
OutImplicit.html:
<html>
<body>
<p><h1>WELCOME TO APACHE TOMCAT SERVER</h1></p>
<% out.print("Today's Date & Current Time :
"+java.util.Calendar.getInstance().getTime());
%>
</body>
</html>

OUTPUT

12
EXPERIMENT – 8
AIM: - Write a JSP Program to show the use of Request Implicit
Object

CODE: -
RequestImplicit.html:
<html>
<body>
<h2>JSP Request Implicit</h2>
<form action="Welcome.jsp">
<label for="fname">Enter Your Name:</label>
<input type="text" name="fname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>

Welcome.jsp:
<%
String name=request.getParameter("fname");
out.print("Welcome "+name);
%>

13
OUTPUT

14
EXPERIMENT – 9
AIM: - Write a JSP Program to show the use of Response Implicit
Object

CODE: -
ResponseImplicit.html:
<html>
<body>
<h2>Welcome to Tomcat Server</h2>
<form action="ResponseImplicit.jsp">
<label for="site">Enter the Word :</label>
<input type="text" name="site">
<input type="submit" value="go"><br/>
</form>
</body>
</html>

ResponseImplicit.jsp:
<%
response.sendRedirect("https://123projectlab.com/online-banking-
system/");
%>

15
OUTPUT

16
EXPERIMENT – 10
AIM: - Write a JSP Program to show the use of Config Implicit
Object

CODE: -
ConfigImplicit.html:
<html>
<body>
<p><h2>Welcome to Tomcat Server</p></h2>
<form action="ConfigImplicit.jsp">
<label for="fname">Enter the Name:</label>
<input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>

web.xml:
<web-app>
<servlet>
<servlet-name>ConfigImplicit</servlet-name>
<jsp-file>/ConfigImplicit.jsp</jsp-file>
<init-param>
<param-name>dname</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ConfigImplicit</servlet-name>
<url-pattern>/ConfigImplicit</url-pattern>
</servlet-mapping>
</web-app>

17
ConfigImplicit.jsp:
<%
out.print("Welcome "+request.getParameter("fname"));
String driver=config.getInitParameter("dname");
out.print("<br>driver name is = " + driver);
%>

OUTPUT

18

You might also like