JSP page forwarding to servlet

Source: Internet
Author: User

A simple example of how JSP pages are forwarded to the servlet, Environment Eclipse, Tomcat

1. The project directory structure is as follows

2. Each part of the code is as follows

1>index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

2>web.xml

<?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" 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 ">  <servlet>    < Servlet-name>forward</servlet-name>    <servlet-class>com.scd.servlet.forwardservlet</ servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>forward</ servlet-name>    <url-pattern>/Forward</url-pattern>  </servlet-mapping></web-app >

3>forwardservlet.java

Package Com.scd.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation Class ForwardServlet */@WebServlet ("/forwardservlet") public class ForwardServlet extends HttpServlet {private static           Final long serialversionuid = 1L;        /** * @see httpservlet#httpservlet () */public ForwardServlet () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method StubSystem.out.println ("My first servlet!");} /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) */protected void DoPost ( HttpservletrEquest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated method stub}} 

Process details: When you click on the forward connection, the request arrives at the server Tomcat, Tomcat checks all the <servlet-mapping></servlet-mapping> in the current project's Web. xml

The property value of <url-pattern></url-pattern> in the configuration. See if either one is the same as the request address forward. The results are found as follows:

<servlet-mapping>

<servlet-name>Forward</servlet-name>

<url-pattern>/Forward</url-pattern>

</servlet-mapping>

Where <url-pattern>/Forward</url-pattern> is your access address

Then Tomcat finds the <servlet-name> <servlet> corresponding to the <servlet-mapping>, and according to the <servlet-name> attribute, Found the same name for the <servlet-name>Forward</servlet>

As follows:

<servlet>

<servlet-name>Forward</servlet-name>

<servlet-class>com.scd.servlet.ForwardServlet</servlet-class>

</servlet>

Find the servlet file Forwardservlet.java according to <servlet-class> above.

Execution results, in terminal output:

JSP page forwarding to servlet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.