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

Servlet and JSP Programming Experiments

The document describes 10 experiments related to servlets and JSPs. Experiment 1 creates a simple servlet that prints a message using the GenericServlet class. Experiment 2 creates a similar servlet but uses the HttpServlet class. Experiment 3 runs an HTML file through Tomcat. Experiment 4 calls a servlet from an HTML file. Experiment 5 calls a servlet using annotations instead of web.xml. Experiment 6 calculates the sum of two numbers in a JSP. Experiment 7 demonstrates the out implicit object in JSP. Experiment 8 demonstrates request parameters in JSP. Experiment 9 demonstrates response redirection in JSP. Experiment 10 demonstrates accessing configuration parameters in JSP.
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 views20 pages

Servlet and JSP Programming Experiments

The document describes 10 experiments related to servlets and JSPs. Experiment 1 creates a simple servlet that prints a message using the GenericServlet class. Experiment 2 creates a similar servlet but uses the HttpServlet class. Experiment 3 runs an HTML file through Tomcat. Experiment 4 calls a servlet from an HTML file. Experiment 5 calls a servlet using annotations instead of web.xml. Experiment 6 calculates the sum of two numbers in a JSP. Experiment 7 demonstrates the out implicit object in JSP. Experiment 8 demonstrates request parameters in JSP. Experiment 9 demonstrates response redirection in JSP. Experiment 10 demonstrates accessing configuration parameters in JSP.
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

Experiment No.

– 1
Aim : Write a servlet program to print a message from Servlet.
( Using GenericServlet )
Program :

[Link] :
import [Link].*;
import [Link].*;

public class MyFirstServlet extends GenericServlet{


public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException{
PrintWriter out=[Link]();
[Link]("Hello , Welcome to the Server ");
}
}
[Link] :
<web-app>
<servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>MyFirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/MyFirstServlet</url-pattern>
</servlet-mapping>
</web-app>

Output :
Experiment No. –2
Aim : Write a servlet program to print a message from Servlet.
( Using HttpServlet )
Program :
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class FirstServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
[Link]("text/html");
PrintWriter out=[Link]();
[Link]("<html><body>");
[Link]("Welcome to servlet");
[Link]("</body></html>");
}}
[Link] :
<web-app>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
</web-app>
Output :
Experiment No. – 3
Aim : Write a .html file and run it through apache tomcat 9.0 Server.
Program :

[Link] :
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Hello Muskan , Welcome to the Servlet</h1>
<h3><p>This is the paragraph Tag</h3>
</body>
</html>

Output :
Experiment No. – 4
Aim : Write a program to call servlet using .html file.
Program :

[Link] :
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello , Welcome to the HTML Page by muskan</h1>
<a href="MyServlet">Click Here</a>
</body>
</html>
[Link] :
import [Link].*;
import [Link].*;
import [Link].*;
public class FirstServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
[Link]("text/html");
PrintWriter out=[Link]();
[Link]("<html><body>");
[Link]("Welcome to servlet");
[Link]("</body></html>");
}}

[Link] :
<web-app>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>

Output :
Experiment No. – 5

Aim : Write a program to call servlet using annotations.


Program :
[Link] :
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

[Link]("text/html");
PrintWriter out=[Link]();

[Link]("<html><body>");
[Link]("<h1>Hello Servlet</h1>");
[Link]("</body></html>");
}
}
Output :
Experiment No. – 6

Aim : Write a JSP program to calculate the sum of two numbers .


Program :

[Link] :
<html>
<head>
<title>Sum of two Numbers</title>
</head>
<body>
<h1>Sum of two Numbers</h1>
<%!
int add(int a,int b){
int sum;
sum = a + b;
return sum;
}
%>
<%= "Sum of two numbers is:"+add(11,11)%>
</body>
</html>
Output :
Experiment No. – 7

Aim : Write a JSP program to show the use of out implicit object.
Program :
[Link] :
<html>
<body>
<% [Link]("Today's date and current time :"+[Link]().getTime());
%>
</body>
</html>

Output :
Experiment No. – 8

Aim : Write a JSP program to show the use of request implicit object.
Program :

[Link] :
<html>
<body>
<h2>HTML Forms</h2>
<form action="[Link]">
<label for="fname">Enter Your Name:</label><br>
<input type="text" name="fname">
<input type="submit" value="go"><br/>
</form>
</form>
</body>
</html>

[Link] :
<%
String name=[Link]("fname");
[Link]("Welcome "+name);
%>
Output :
Experiment No. – 9

Aim : Write a JSP program to show the use of response implicit


object.
Program :
[Link] :
<html>
<body>
<h2>JSP implicit response </h2>
<form action="[Link]">
<label for="fname">Enter Your Name:</label><br>
<input type="text" name="fname">
<input type="submit" value="go"><br/>
</form>
</form>
</body>
</html>
[Link] :
<%
[Link]("[Link]
%>

Output :
Experiment No. – 10

Aim : Write a JSP program to show the use of config implicit object.
Program :
[Link] :
<html>
<body>
<form action="welcome">
<input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

[Link] :
<web-app>
<servlet>
<servlet-name>welcome</servlet-name>
<jsp-file>/[Link]</jsp-file>
<init-param>
<param-name>dname</param-name>
<param-value>[Link]</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
[Link] :
<%
[Link]("Welcome "+[Link]("fname"));
String driver=[Link]("dname");
[Link]("<br>driver name is = " + driver);
%>

Output :

You might also like