0% found this document useful (0 votes)
17 views4 pages

Using Request Attributes

The document contains an HTML form for users to input their name, which is processed by a Java servlet named SetDataServlet. This servlet retrieves the name and sets it as a request attribute before forwarding the request to another servlet, GetDataServlet, which displays a welcome message. The web application is configured with a web.xml file that maps the servlets to their respective URL patterns.
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)
17 views4 pages

Using Request Attributes

The document contains an HTML form for users to input their name, which is processed by a Java servlet named SetDataServlet. This servlet retrieves the name and sets it as a request attribute before forwarding the request to another servlet, GetDataServlet, which displays a welcome message. The web application is configured with a web.xml file that maps the servlets to their respective URL patterns.
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

<!

DOCTYPE html>

<html>

<head>

<title>Request Attributes Example</title>

</head>

<body>

<h2>Enter Your Name</h2>

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

<label>Your Name: </label>

<input type="text" name="userName" required><br><br>

<input type="submit" value="Submit">

</form>

</body>

</html>

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

import [Link].*;

public class SetDataServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {

// Get the user input from the form

String userName = [Link]("userName");

// Set the user name in the request attribute

[Link]("userName", userName);

// Forward the request to the next servlet

RequestDispatcher dispatcher =
[Link]("/getDataServlet");

[Link](request, response);

import [Link].*;

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

public class GetDataServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

// Retrieve the user name from the request attribute

String userName = (String) [Link]("userName");

// Set the content type for the response

[Link]("text/html");

// Print the response

PrintWriter out = [Link]();

[Link]("<html><body>");

if (userName != null) {

[Link]("<h2>Welcome, " + userName + "!</h2>");

} else {

[Link]("<h2>No user name found in the request.</h2>");

[Link]("</body></html>");

<web-app xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link] version="3.0">
<servlet>

<servlet-name>SetDataServlet</servlet-name>

<servlet-class>SetDataServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SetDataServlet</servlet-name>

<url-pattern>/setDataServlet</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>GetDataServlet</servlet-name>

<servlet-class>GetDataServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>GetDataServlet</servlet-name>

<url-pattern>/getDataServlet</url-pattern>

</servlet-mapping>

</web-app>

You might also like