熟悉servlet的页面跳转
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的页面跳转的更多相关文章
- Jsp与servlet之间页面跳转及参数传递实例(转)
原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...
- Servlet的页面跳转
Servlet的跳转 内部跳转 req.getRequestDispatcher() Server--->AServlet--->BServlet 两个S ...
- 【转】(超详细)jsp与servlet之间页面跳转及参数传递实例
初步学习JavaEE,对其中jsp与Servlet之间的传值没弄清楚,查看网上资料,发现一篇超详细的文章,收获大大,特此记录下来.具体链接:http://blog.csdn.net/ssy_shand ...
- java servlet 几种页面跳转的方法及传值
java servlet 几种页面跳转的方法及传值 java web 页面之间传值有一下这几种方式1.form 表单传递参数2.url地址栏传递参数3.session4.cookie5.appli ...
- Servlet、JSP中页面跳转的方式
一.Servlet:当然,在servlet中,一般跳转都发生在doGet, doPost等方法里面.1) redirect 方式response.sendRedirect("success ...
- Servlet页面跳转实现方法的区别
一直对Servlet页面跳转的几种方式理解的糊里糊涂的,今天在网上搜了一把,找到一遍比较好的,记下来,以后看看. Servlet页面跳转分两部分,一是发生在Servlet,一是在JSP,其实JSP也就 ...
- 乱码问题-页面跳转方式-Servlet配置文件
1.HttpServletRequest a)HttpServletRequest是一个接口,继承了ServletRequest接口: b)HttpServletRequest对象由服务器创建,并作为 ...
- jsp/servlet页面跳转丢失样式问题
问题:使用servlet,如何处理在多路径页面跳转中servlet转发页面样式丢失问题?(例如访问 http://localhost/project/listUser.action后转到http:// ...
- JSP、Servlet中的相对路径和绝对路径 页面跳转问题
转自:http://blog.csdn.net/wym19830218/article/details/5503533/ 1.JSP.Servlet中的相对路径和绝对路径 前提:假设你的Http地址为 ...
随机推荐
- Ubuntu14.04如何用root账号登陆系统
在虚拟机VMWARE中安装完Ubuntu后,只能用新建的普通用户登陆,很不方便做实验:那如何用root用户登陆账号呢? (1)用普通账号登陆,打开终端terminal: (2)在terminal的输入 ...
- Python-Redis的String操作
Ubuntu安装Redis sch01ar@ubuntu:~$ sudo apt install redis-server sch01ar@ubuntu:~$ redis-server sch01ar ...
- java中的equals方法
这个方法首先比较的是两个对象的地址是否相同,如果相同直接返回true, 否则, (1)如果是string类型的先比较是否是string类型,是的话,再比较是否长度相同,相同的话再比较,每个字符是否相同 ...
- Jumony.Core非常厉害的一个开源项目!
简单的说,就是解析html文档的,以前发送一个get请求获取一个页面的html文本后,想要获取里面的数据都是使用正则表达式.(非常的苦逼), 现在用这个获取就very easy! 安装的话在Nu Ge ...
- LAMP 1.6 Discuz安装
1.下载 ...
- DESede/CBC/PKCS5Padding
Java.security.NoSuchAlgorithmException: Cannot find any provider supporting DESede/CBC/PKCS5Padding ...
- [Linux]关于sigprocmask函数的讨论
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...
- Linux系统及lvm知识
一.磁盘分区是怎样表示的 IDE磁盘的设备文件采用/dev/hdx 来命名,分区则采用/dev/hdxy来命名,其中想表示磁盘(a是第一块磁盘,b是第二块磁盘,以此类推),与代表分区的号码(由1开始, ...
- [CentOS7] Segmentation fault (core dumped),但是在主机上找不到core文件
1.问题描述 程序执行报:Segmentation fault (core dumped),但是在主机上找不到core文件 2.如何让系统生成core file /home>ulimit -ac ...
- 51nod1138(连续和)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1138 题意:中文题诶- 思路:假设 x=a1+(a1+1)+ ...

