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

Using URL Rewriting

The document contains a simple web application that demonstrates URL rewriting using servlets. It includes an HTML form for users to input their name, which is then processed by a servlet that redirects to another servlet, appending the name to the URL. The second servlet retrieves the name from the URL and displays a greeting message to the user.
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)
9 views4 pages

Using URL Rewriting

The document contains a simple web application that demonstrates URL rewriting using servlets. It includes an HTML form for users to input their name, which is then processed by a servlet that redirects to another servlet, appending the name to the URL. The second servlet retrieves the name from the URL and displays a greeting message to the user.
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>URL Rewriting Example</title>

</head>

<body>

<h2>Enter Your Name</h2>

<form action="setUrlDataServlet" 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 SetUrlDataServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException, IOException {

// Get the user's name from the form

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

// Redirect to another servlet and append the data to the URL

[Link]("getUrlDataServlet?userName=" +
userName);

import [Link].*;

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

public class GetUrlDataServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

// Get the userName parameter from the URL

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

// Set content type for the response

[Link]("text/html");

// Print the response

PrintWriter out = [Link]();

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

if (userName != null) {

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

} else {

[Link]("<h2>No data found in the URL.</h2>");

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

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

<servlet>

<servlet-name>SetUrlDataServlet</servlet-name>

<servlet-class>SetUrlDataServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SetUrlDataServlet</servlet-name>

<url-pattern>/setUrlDataServlet</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>GetUrlDataServlet</servlet-name>

<servlet-class>GetUrlDataServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>GetUrlDataServlet</servlet-name>

<url-pattern>/getUrlDataServlet</url-pattern>

</servlet-mapping>

</web-app>

You might also like