码迷,mamicode.com
首页 > Web开发 > 详细

jsp页面转发到servlet

时间:2016-09-04 11:35:58      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

      一个简单的例子来了解一下jsp页面转发到servlet的过程,环境 eclipse、tomcat

1.工程目录结构如下

    技术分享

2.各部分代码如下

   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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>首页</title>
</head>
<body>
 <a href = "Forward">Forward</a>
</body>
</html>

   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 response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.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
		
	}

}

        过程详解:当点击Forward连接时,请求到达服务器tomcat后,tomcat检查当前项目的web.xml中所有的<servlet-mapping></servlet-mapping>

配置中的<url-pattern></url-pattern>的属性值。看当中有没有一个和请求地址Forward相同。结果找到如,如下:

  <servlet-mapping>

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

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

  </servlet-mapping>

   其中  <url-pattern>/Forward</url-pattern>  是你访问地址

然后,tomcat找到和<servlet-mapping>相对应的<servlet-name>的<servlet>,根据<servlet-name>属性匹配, 找到了相同名字的<servlet-name>Forward</servlet>

如下:

 <servlet>

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

    <servlet-class>com.scd.servlet.ForwardServlet</servlet-技术分享class>

  </servlet>

根据上面的<servlet-class>找到Servlet文件ForwardServlet.java。

 

执行结果,在终端输出:

技术分享

 

jsp页面转发到servlet

标签:

原文地址:http://www.cnblogs.com/shootercheng/p/5838737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!