jsp-1 熟悉servlet的jsp页面跳转

jspweb里面用到的servlet跳转页面的方法

 使用的jar包只有

commons-lang3-3.5.jar

运行时,tomcat会先根据web.xml里面的信息,查找servlet

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>servlet</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
      <servlet-name>servlet</servlet-name>
      <servlet-class>com.javaweb.action.Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>servlet</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  </web-app>
<servlet> 就是你注册的servlet和他的物理地址
<servlet-mapping>servlet的相对地址,就是在.jsp中怎么用

然后就是根据欢迎页面index.jsp等待用户操作
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();// 获得当前的项目根目录路径
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
    //完整路径
%>
<!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>
<table border=0 cellpadding=0 cellspacing=0  style="margin:auto;border-collapse:separate; border-spacing:10px;">
<tr>
    <td>
        servlet根目录路径:<%out.print(path);%>
    </td>
</tr>
<tr>
    <td>
        servlet完整路径:<%out.print(basePath);%>
    </td>
</tr>
<tr>
    <td>
        <!--后缀名是.do的直接根据目录找到first方法-->
    <a href="<%=basePath%>/first.do">第一的英文</a>
    </td>
</tr>
<tr>
    <td>
        <!--?的是-->
    <a href="<%=basePath%>/.do?op=second">第二的英文</a>
    </td>
</tr>
<tr>
    <td>
    <!--触发?的else选项,常用来放错误信息-->
    <a href="<%=basePath%>/.do?op=WOSUIBIANDADE">第三的英文</a>
    </td>
</tr>
</table>
</body>
</html>

servlet的具体响应

package com.javaweb.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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

import org.apache.commons.lang3.StringUtils;

public class Servlet extends HttpServlet{

    /**
     * 用于版本控制
     */
    private static final long serialVersionUID = -2357925750878300415L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //纯碎是用来判断有没有错误
        String url=req.getServletPath();
        String method=url.substring(1,url.lastIndexOf("."));
        try {
            Method met=getClass().getDeclaredMethod(method, HttpServletRequest.class,HttpServletResponse.class);
            try {
                met.invoke(this, req,resp);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        //使用?跳转页面
        req.setCharacterEncoding("UTF-8");
        String op=req.getParameter("op");
        if(StringUtils.isNotBlank(op)){
            if("second".equalsIgnoreCase(op)){
                second(req, resp);
            }else{
               PrintWriter out=resp.getWriter();//调用窗口
                out.println("THIRD");

} } }
public void first(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("first.jsp"); } public void second(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("second.jsp"); } }

显然用?的方法和用.do的方法都能实现同样的功能

但是在大量方法同时存在的时候?方法可以用于区分不同方面的方法

req.getParameter("login");
req.getParameter("logout");....

结果
 







posted @ 2017-01-14 16:17  lgp20151222  阅读(323)  评论(0编辑  收藏  举报