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



jsp-1 简单的应用servlet,并用其跳转页面的更多相关文章

  1. 使用Eclipse开发Web项目(JSP)——简单登录、无sql

    1.使用Eclipse开发Web项目(JSP) tomcat 2.在Eclipse中创建的Web项目: 浏览器可以直接访问webContent中的文件 例如http://localhost:8080/ ...

  2. JSP简单练习-用Servlet获取表单数据

    // javaBean代码 package servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.* ...

  3. JSP制作简单登陆

    JSP制作简单登陆界面 运行环境 eclipse+tomcat+MySQL 不知道的可以参考Jsp运行环境--Tomcat 项目列表 这里我先把jsp文件先放在Web-INF外面访问 需要建立的几个文 ...

  4. JSP的简单介绍

    什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写htm ...

  5. jsp中获取不到servlet的cookie

    今天做登陆,发现jsp中使用document.cookie获取不到servlet生成的cookie,我们可以在浏览器的cookie文件夹中发现,servlet中生成的cookie和jsp中的生成的路径 ...

  6. 新手详解JAVA+数据库+JSP完成简单页面

    本篇以数据库添加为例(本例中数据库名为“xinxi”表单名字为“stud”) 准备---实体层: package entity; public class Student { private Stri ...

  7. JSP源码、改写Servlet为JSP、查看转译成为Servlet的文件、JSP字符编码设置

    概述 在Servlet中编写HTML太麻烦了,应该使用JSP.JSP中可以直接编写HTML,使用指示.声明.脚本(scriptlet)等元素来堆砌各种功能,但JSP最后还是会被容器转译为Servlet ...

  8. 在jsp中接收并处理servlet传过来的含有bean的List

    在jsp中接收并处理servlet传过来的含有bean的List 例如有以下bean package com.test.domain; class Student{ private Stirng na ...

  9. JSP内置对象与servlet对应关系

    隐式对象 说明 out 转译后对应JspWriter对象,其内部关联一个PringWriter对象 request 转译后对应HttpServletRequest/ServletRequest对象 r ...

随机推荐

  1. OpenSUSE13.2安装MongoDB

    真是一个悲伤的故事,就是你解决过得问题没有记住,却需要再通过搜索引擎来找一遍,幸运的是曾经你做过记录,搜索帮你找到了. 这是我一个Wordpress博客整理记录的,好久没在那里更新了,两个月的时间,我 ...

  2. leetcode第20题--Valid Parentheses

    Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

  3. 使用SQL Server 2005数据库管理工具 - 初学者系列 - 学习者系列文章

    本文讲述使用SQL Server 2005 Express数据库管理工具的使用. 1.打开数据库管理工具 2.选择下面的SQL Server 身份验证,因为在安装数据库的时候设置了sa的密码. 3.选 ...

  4. bash元字符(上)

    元字符 行动 样例 回车换行 结束一个命令 空格 切割命令行中的元素 ls /etc Tab 命令自己主动补全 # 開始一行凝视 #This is a comment line " 引用多个 ...

  5. DDD 应对具体业务场景,Domain Model 重新设计

    DDD 应对具体业务场景,Domain Model 重新设计 写在前面 上联:no zuo no die why you try 下联:no try no high give me five 横批: ...

  6. MVC与Validate验证提示的样式修改

    MVC中使用Validate的验证,要修改错误提示样式一共有3处需要修改,否则就不太完美了: MVC中的Validate的验证机制只用在后台写一次,就可以完成前台和后台的完美验证,前台的验证主要是依靠 ...

  7. 由link()和symlink()谈到软链接与硬链接

    任何一个文件可以有多个目录项指向其i节点.创建一个向现存文件连接的方法是使用l i n k函数. #include <unistd.h> int link(const char * e x ...

  8. visual studio快捷键总结

    熟练操作vs的快捷键,可以有效地提高开发效率,下面将vs 2008与vs 2010的快捷键进行了总结,结果如下表: 注:vs 2010与vs 2008的快捷键基本相同. 编辑:   CTRL + M, ...

  9. Indenting source code

    Artistic Style 1.15.3 A Free , Fast and Small Automatic Formatterfor C , C++ , C# , Java Source Code ...

  10. [Usaco2008 Feb]Meteor Shower流星雨[BFS]

    Description 去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的 ...