程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> jsp-1 簡單的應用servlet,並用其跳轉頁面,jsp-1servlet

jsp-1 簡單的應用servlet,並用其跳轉頁面,jsp-1servlet

編輯:JAVA綜合教程

jsp-1 簡單的應用servlet,並用其跳轉頁面,jsp-1servlet


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

結果
 







  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved