Explain Url rewriting using Servlet and JSP with example?


Url rewriting is a process of appending or modifying any url structure while loading a page.

 

Example

Servlet Code

package com.candidjava;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegistrationController
 */
public class RegistrationController extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String name=request.getParameter("name");
		String email=request.getParameter("email");
		
		if(name.trim().length()==0)
		{
			response.sendRedirect("registration.jsp?status=Name cannot be empty");
			return;
		}
		if(email.trim().length()==0)
		{
			response.sendRedirect("registration.jsp?status=Email cannot be empty");
			return;
		}
		response.sendRedirect("success.html");
		
	}

}

View Page

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		String msg = request.getParameter("status");
		if (msg == null) {
			msg = "";
		}
	%>

	Candidjava Url rewritting example

	<p style="color: red;">
		<%=msg%>
	</p>
	<form action="RegistrationController" method="post">
		Name : <input type="text" name="name"> <br> 
		Email : <input type="text" name="email"> <br> 
		<input type="submit" value="register">
	</form>


</body>
</html>


Screenshot

             

                

            


Download

url rewriting examplewar

url rewriting examplezip




Comments


©candidjava.com