October 07, 2013

How to redirect to second page from first page through servlet

Servlet Mapping example in deployment descriptor


This java server page (jsp) example shows how to redirect from a page another page in jsp, the request from jsp page will pass through a servlet and from the servlet to second . the following java code will decide which page the request should be directed : page.response.sendRedirect("secondPage.jsp");

web.xml file



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Artificial intelligence</display-name>
  <welcome-file-list>
    <welcome-file>newPage.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description>An example for servlet mapping in jsp servlet</description>
    <display-name>Beyonce superbowl</display-name>
    <servlet-name>Beyonce</servlet-name>
    <servlet-class>com.NeuralNetwork.servlet.EvasiOn</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Beyonce</servlet-name>
    <url-pattern>/NeuralNetwork.jsp</url-pattern>
  </servlet-mapping>
</web-app>





first page



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> groundHog see his shadow</title>
</head>
<body>
<H1> new page !!! Indexing Terms </H1>
<a href="NeuralNetwork.jsp">click here</a>
</body>
</html>


second page



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Did the groundHog see his shadow</title>
</head>
<body>
<h1> This is second page !!! </h1>
</body>
</html>

servlet



package com.NeuralNetwork.servlet;

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 EvasiOn
 * @author fuzzyLogic
 */
public class EvasiOn extends HttpServlet {
    private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EvasiOn() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(" inside Test Servlet");
        response.sendRedirect("secondPage.jsp");
    }

}


Please provide your feed back.... 

2 comments:

  1. Hi everyone, it's my first pay a quick visit at this web site, and article is genuinely fruitful designed for me, keep up posting these types of content.

    Take a look at my website: creative group
    My website > grupa kreatywna

    ReplyDelete
    Replies
    1. Thanks, Please provide your name.

      Delete

Your feedback may help others !!!

Facebook comments