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

Ajp - Notes 1

The document outlines two servlet exercises: the first demonstrates handling form data with a login form, while the second shows how to display request headers received by a servlet. Each exercise includes a step-by-step procedure, relevant Java code, and confirms successful execution. The first program captures username and password inputs, and the second retrieves and displays all request headers.

Uploaded by

mei
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)
1 views10 pages

Ajp - Notes 1

The document outlines two servlet exercises: the first demonstrates handling form data with a login form, while the second shows how to display request headers received by a servlet. Each exercise includes a step-by-step procedure, relevant Java code, and confirms successful execution. The first program captures username and password inputs, and the second retrieves and displays all request headers.

Uploaded by

mei
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: 7 HANDLE FORM DATA USING SERVLET

AIM:
To create a program using java servlet to handle form data.

PROCEDURE:

Step 1: Start the process

Step 2: Create a form with the fields username and password, and action as

loginservlet.

Step 3: Create a web application using eclipse and name as loginservlet

Step 4: Create and link the variables into html variables.

Step 5: Using the writer.println function display the information entered in the form.

Step 6: Run the program

Step 7: Stop the process.

1
PROGRAM:

Loginform.html

<form name="loginForm" method="post" action="loginServlet">


Username: <input type="text" name="username"/> <br/>
Password: <input type="password" name="password"/> <br/>
<input type="submit" value="Login" />
</form>

Loginservlet

package net.codejava.servlet;
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("/loginServlet")
public class LoginServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// read form fields
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: " + username);
System.out.println("password: " + password);

// do some processing here...


// get response writer

2
PrintWriter writer = response.getWriter();
// build HTML code
String htmlRespone = "<html>";
htmlRespone += "<h2>Your username is: " + username + "<br/>";
htmlRespone += "Your password is: " + password + "</h2>"; htmlRespone +=
"</html>";
// return response writer.println(htmlRespone);
}
}

3
OUTPUT:

4
RESULT:

Thus the above program has been executed successfully.

5
EX. NO: 8. TABLE HEADERS USING SERVLET

AIM:
To Write a simple Servlet program to create a table of all the headers it
receives along with their associated values.

PROCEDURE:

Step 1: Start the process


Step 2: Create a web application by choosing file-new project-web-web application.
Step 3: To get a method use request.getmethod()
Step 4: To get a header information request.getheader function is used
Step 5: Use try, catch, throw function to display the information
Step 6: Run the program
Step 7: Stop the process

6
PROGRAM:

ShowRequestHeaders.java
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/** Shows all the request headers sent on the current request. */

public class ShowRequestHeaders extends HttpServlet


{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Servlet Example: Showing Request Headers";
String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n"; out.println(docType
+ "<HTML>\n"
+ "<HEAD><TITLE>"
+ title +"</TITLE></HEAD>\n"
+ "<BODY BGCOLOR=\"#FDF5E6\">\n"
+ "<H1 ALIGN=\"CENTER\">"
+ title + "</H1>\n"
+ "<B>Request Method: </B>"
+ request.getMethod() + "<BR>\n"
+ "<B>Request URI: </B>"
+ request.getRequestURI()
+ "<BR>\n" + "<B>Request Protocol: </B>"
+ request.getProtocol() + "<BR><BR>\n"
+ "<TABLE BORDER=1 ALIGN=\"CENTER\">\n"

7
+ "<TR BGCOLOR=\"#FFAD00\">\n"
+ "<TH>Header Name<TH>Header Value");

Enumeration headerNames = request.getHeaderNames();


while(headerNames.hasMoreElements())
{
String headerName = (String)headerNames.nextElement();
out.println("<TR><TD>" + headerName);
out.println(" <TD>" + request.getHeader(headerName) );
}
out.println("</TABLE>\n</BODY></HTML>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}

8
OUTPUT:

9
RESULT:

Thus the above program has been executed successfully.

10

You might also like