0% found this document useful (0 votes)
2 views

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<!

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 java.io.*;
import javax.servlet.*;

import javax.servlet.http.*;

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 = request.getParameter("userName");

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

response.sendRedirect("getUrlDataServlet?userName=" +
userName);

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class GetUrlDataServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {

// Get the userName parameter from the URL

String userName = request.getParameter("userName");

// Set content type for the response

response.setContentType("text/html");

// Print the response

PrintWriter out = response.getWriter();

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

if (userName != null) {

out.println("<h2>Hello, " + userName + "!</h2>");

} else {

out.println("<h2>No data found in the URL.</h2>");

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

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 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