jsp页面转发到servlet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp页面转发到servlet相关的知识,希望对你有一定的参考价值。

      一个简单的例子来了解一下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的主要内容,如果未能解决你的问题,请参考以下文章

如何转发到java Servlet中JSP页面的特定部分

Web_Servlet和jsp页面数据交互,通过请求转发在jsp中显示数据

jsp重定向和转发

Servlet_问题总结

servlet转发动作时如何触发javascript函数

servlet中sendRedirect重定向问题