0% found this document useful (0 votes)
83 views10 pages

CS8581 Web Technology

This document describes how to invoke a Java servlet using an HTML form. It includes code for an HTML form page, a servlet class, and web.xml configuration file. The HTML form collects user input and submits it to the servlet. The servlet uses the getParameter method to retrieve the form data and write it back to the response. Session tracking is also demonstrated, with the servlet storing user data in the HTTP session and a second servlet page able to retrieve and display it.

Uploaded by

shibu
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)
83 views10 pages

CS8581 Web Technology

This document describes how to invoke a Java servlet using an HTML form. It includes code for an HTML form page, a servlet class, and web.xml configuration file. The HTML form collects user input and submits it to the servlet. The servlet uses the getParameter method to retrieve the form data and write it back to the response. Session tracking is also demonstrated, with the servlet storing user data in the HTTP session and a second servlet page able to retrieve and display it.

Uploaded by

shibu
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/ 10

Ex No: 5

INVOKING SERVLETS USING HTML FORM

AIM

To invoke a java Servlet program using html form.

ALGORITHM

 Identify the classes to be used.

 Define a class that should extend the one of the base class httpServlet.

 The service( ) method need to be coded to receive information from the browser
whenever a client browser makes a request for the HTML page.

 Generate the source and compile it.

 Deploy the Servlet then execute the Servlet.

 To execute the program open the browser and then type:

http://localhost:8080/sample/invokeServlet.html
PROGRAM

invokeServlet.html

<html>
<head>
<title>Student Information Form</title>
</head>
<body>
<center>
<form name="form1" action="http://localhost:8080/sample/my_servletDemo">
<h3>Enter student information in following fields -</h3>
<table>
<tr>
<td><b>Roll Number</b></td>
<td><input type="text" name="Roll Number" size="25" value=" "></td>
</tr>

<tr>
<td><b>Student Name</b></td>
<td><input type="text" name="Student Name" size="25" value=" "></td>
</tr>

<tr>
<tr>
<td><b>Student Address</b></td>
<td><input type="text" name="Address" size="50" value=" "></td>
</tr>

<tr>
<td><b>Phone</td>
<td><input type="text" name="Phone" size="25" value=" "></td>
</tr>

<tr>
<td><b>Total Marks</td>
<td><input type="text" name="Total Marks" size="10" value=" "></td>
</tr>

</table>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>

my_servletDemo.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
public class my_servletDemo extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res) throws
ServletException,IOException
{
PrintWriter out=res.getWriter();
Enumeration en=req.getParameterNames();
while(en.hasMoreElements())
{
String name_received=(String)en.nextElement();
out.print(name_received+ " = ");
String value_received=req.getParameter(name_received);
out.println(value_received);
out.println(" ");
}
out.close();
}
}

Web.xml

<web-app>

<!-- Servlet-definitions -->

<servlet>
<servlet-name>my_servletDemo </servlet-name>
<servlet-class>my_servletDemo </servlet-class>
</servlet>

<!-- Servlet-mappings -->

<servlet-mapping>
<servlet-name>my_servletDemo </servlet-name>
<url-pattern>/my_servletDemo </ url-pattern >
</servlet- mapping >

</web-app>

Steps to execute Servlets:

1. Open Tomcat6.0 program directory.

2. Double click webapps folder.

3. Create a folder named sample.

4. Place the invokeServlet.html file into the sample folder.

5. Create a folder named WEB-INF in the sample folder.

6. Place the web.xml file in the WEB-INF folder.

7. Create a folder named classes in the WEB-INF folder.

8. Place the class file of the java program my_servletDemo into the classes folder in the
WEB-INF folder.
OUTPUT

http://localhost:8080/sample/invokeServlet.html
CONCLUSION

Thus the program for invoking Servlets from the html forms has been executed.

Ex 5(ii) session tracking

Index.html

<html>

<head>

<title>session tracking</title>

</head>

<body>

<form action="FirstServlet" method="POST" >

Name:<input type="text" name="userName"/><br/>

<input type="submit" value="go"/>

</form>

</body>
</html>

FirstServlet.java

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

public class FirstServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String n=request.getParameter("username");
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet page1</title>");
out.println("</head>");
out.println("<body>");
out.println("welcome" +n+"<br>");
HttpSession session=request.getSession();
session.setAttribute("uname", n);
out.println("<a href='SecondServlet'>visit</a>");
out.println("</body>");
out.println("</html>");
}
}
}

SecondServlet.java

import java.io.IOException;

import java.io.PrintWriter;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.*;

public class page2 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

HttpSession session=request.getSession(false);

String n=(String)session.getAttribute("uname");

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet page2</title>");

out.println("</head>");

out.println("<body>");

out.println("hello" +n);

out.println("</body>");

out.println("</html>");

}
}

You might also like