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");....
结果
 



熟悉servlet的页面跳转的更多相关文章

  1. Jsp与servlet之间页面跳转及参数传递实例(转)

    原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...

  2. Servlet的页面跳转

    Servlet的跳转    内部跳转 req.getRequestDispatcher()        Server--->AServlet--->BServlet        两个S ...

  3. 【转】(超详细)jsp与servlet之间页面跳转及参数传递实例

    初步学习JavaEE,对其中jsp与Servlet之间的传值没弄清楚,查看网上资料,发现一篇超详细的文章,收获大大,特此记录下来.具体链接:http://blog.csdn.net/ssy_shand ...

  4. java servlet 几种页面跳转的方法及传值

    java servlet 几种页面跳转的方法及传值   java web 页面之间传值有一下这几种方式1.form 表单传递参数2.url地址栏传递参数3.session4.cookie5.appli ...

  5. Servlet、JSP中页面跳转的方式

    一.Servlet:当然,在servlet中,一般跳转都发生在doGet, doPost等方法里面.1)  redirect 方式response.sendRedirect("success ...

  6. Servlet页面跳转实现方法的区别

    一直对Servlet页面跳转的几种方式理解的糊里糊涂的,今天在网上搜了一把,找到一遍比较好的,记下来,以后看看. Servlet页面跳转分两部分,一是发生在Servlet,一是在JSP,其实JSP也就 ...

  7. 乱码问题-页面跳转方式-Servlet配置文件

    1.HttpServletRequest a)HttpServletRequest是一个接口,继承了ServletRequest接口: b)HttpServletRequest对象由服务器创建,并作为 ...

  8. jsp/servlet页面跳转丢失样式问题

    问题:使用servlet,如何处理在多路径页面跳转中servlet转发页面样式丢失问题?(例如访问 http://localhost/project/listUser.action后转到http:// ...

  9. JSP、Servlet中的相对路径和绝对路径 页面跳转问题

    转自:http://blog.csdn.net/wym19830218/article/details/5503533/ 1.JSP.Servlet中的相对路径和绝对路径 前提:假设你的Http地址为 ...

随机推荐

  1. 基于ftp服务实现yum网络共享

    安装ftp服务:yum install vsftpd 安装后: CentOS7 启动服务:systemctl start vsftpd 设置开机启动:systemctl enable vsftpd 同 ...

  2. C语言学习笔记--enum和sizeof关键字

    1.enum关键字 C语言中enum关键字用来定义枚举类型 (1)enum 是 C 语言中的一种自定义类型(2)enum 值是可以根据需要自定义的的整型值(3)第一个定义的 enum 值默认为 0 ( ...

  3. [51nod1101]换零钱

    题意:给定钱,计算其能换成零钱的分类种数. 解题关键:完全背包计数. $dp[i][j]$表示前i个物品构成j元的种类数,然后优化一维. #include<bits/stdc++.h> u ...

  4. Spring开发包介绍

    -----------------siwuxie095                         核心开发包         建立 Spring 工程时,需要引入 Spring 的开发包,否则无 ...

  5. Flask11 Session、CSRF、注销session、利用端点自动跳转

    1 怎么对存储的cookie数据进行加密 利用response对象去设置cookie时,存储到浏览器中的cookie数据都是明文的,容易被一些计算机爱好者利用:利用session存的cookie数据可 ...

  6. layui 表格新增删除一行

    1.html 代码 <div class="layui-row layui-col-space5"> <div> <span style=" ...

  7. 正则表达式需要匹配的内容本身就自带了html转义字符,需要转义,否则无法匹配

    1.正则表达式需要匹配的内容本身就自带了html转义字符,需要转义,否则无法匹配 例如,对于"requestNo\":\"3b89957436eaacd8311535e0 ...

  8. netty+mqtt

    package io.mqtt.server; import io.mqtt.tool.ConfigService;import io.netty.bootstrap.ServerBootstrap; ...

  9. [转][译]ASP.NET MVC 4 移动特性

    此教程将讨论ASP.NET MVC 4 Web应用程序里的移动特性.对于此教程,可以使用 Visual Studio Express 2012 或者 Visual Web Developer 2010 ...

  10. Redis学习笔记(一):基础数据结构

    一. 引言 <Redis设计与实现>一书主要分为四个部分,其中第一个部分主要讲的是Redis的底层数据结构与对象的相关知识. Redis是一种基于C语言编写的非关系型数据库,它的五种基本对 ...