JSP小结-Servlet请求与路径

1.JSP指定输出格式

1.1–html格式

<%@ page contentType="text/html;charset=UTF-8";language="java"%>

1.2–Excel格式

<% response.setHeader("Content-disposition","attachment;filename=result.xls");
%>

<%@ page contextType="application/vnd.ms-Excel;charset=UTF-8";language="java"%>

1.3–Word格式

<% response.setHeader("Content-disposition","attachment;filename=result.word");
%>

<%@ page contextType="application/vnd.ms-Word;charset=UTF-8";language="java"%>

如果需要输出格式为Excel或Word,则应将JSP页面的 JS 和CSS 元素删除掉。


2.JSP与Servlet

Servlet是一个Java类,采用CGI方式逐句输出HTML语句,主要用于业务逻辑层,实现业务逻辑的处理。
JSP将Java代码嵌套在HTML中,简化和方便了网页的设计和修改。是为实现表示层而设计的,主要完成界面的显示逻辑。


3.JSP与Servlet请求与路径

jsp:testPath1.jsp

servlet:TestPath url-pattern:/testPath

testPath1.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
//一定要引入包

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

        <%  
            out.println("request.getRequestURI():"+request.getRequestURI()+"
"
); out.println("request.getRequestURL():"+request.getRequestURL()+"
"
); String strPathFile = application.getRealPath(request.getRequestURI()); out.print("文件的绝对路径:"+strPathFile+"
"
); String strDirPath = new File(application.getRealPath(request.getRequestURI())).getParent(); out.println("目录的绝对路径:"+strDirPath+"
"
); %>
<a href="testPath">testPath Servleta> body> html>

请求testPath1.jsp浏览器显示

request.getRequestURI():/SSM-2/testPath1.jsp
request.getRequestURL():http://localhost:8088/SSM-2/testPath1.jsp

**application.getRealPath(request.getRequestURI())**
文件的绝对路径:D:\apache-tomcat-7.0.40\webapps\SSM-2\SSM-2\testPath1.jsp

** new File(application.getRealPath(request.getRequestURI())).getParent()**
目录的绝对路径:D:\apache-tomcat-7.0.40\webapps\SSM-2\SSM-2

请求 /testPath Servlet

java代码:

package com.web.test3;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

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

public class TestPath extends HttpServlet {
    /**
     * Destruction of the servlet. 
*/
public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); System.out.println("request.getLocalAddr()--:"+request.getLocalAddr()); System.out.println("request.getLocalName()--:"+request.getLocalName()); System.out.println("request.getLocalPort()--:"+request.getLocalPort()); System.out.println("request.getLocales()--:"+request.getLocales()); System.out.println("request.getRemoteAddr()--:"+request.getRemoteAddr()); System.out.println("request.getRemoteHost()--:"+request.getRemoteHost()); System.out.println("request.getRemotePort()--:"+request.getRemotePort()); System.out.println("request.getRequestedSessionId()--:"+request.getRequestedSessionId()); System.out.println("request.getRequestURI()--:"+request.getRequestURI()); System.out.println("request.getRequestURL()--:"+request.getRequestURL()); System.out.println("request.getServerName()--:"+request.getServerName()); System.out.println("request.getServerPort()--:"+request.getServerPort()); System.out.println("request.getServletPath()--:"+request.getServletPath()); System.out.println("request.getContextPath()--:"+request.getContextPath()); System.out.println("request.getContentLength()--:"+request.getContentLength()); System.out.println("request.getServletPath():"+request.getServletPath()); out.println("request.getServletPath():"+request.getServletPath()+"
"
); System.out.println("request.getSession().getServletContext().getResourcePaths('/')"+request.getSession().getServletContext().getResourcePaths("/")); System.out.println("request.getSession().getServletContext().getServletContextName()"+request.getSession().getServletContext().getServletContextName()); System.out.println("getServletConfig().getServletName()"+getServletConfig().getServletName()); System.out.println("getServletConfig().getServletContext().getContextPath()"+getServletConfig().getServletContext().getContextPath()); String strPathFileString = request.getSession().getServletContext().getRealPath(request.getRequestURI()); System.out.println("文件的绝对路径:"+strPathFileString); out.println("文件的绝对路径:"+strPathFileString+"
"
); String strDirPath = new File(request.getSession().getServletContext().getRealPath(request.getRequestURI())).getParent(); System.out.println("目录的绝对路径:"+strDirPath); out.println("目录的绝对路径:"+strDirPath+"
"
); //获取web项目的全路径 String strFullPath = getServletContext().getRealPath("/"); System.out.println("获取web项目的全路径"+strFullPath); out.print("获取web项目的全路径---getServletContext().getRealPath('/'):"+strFullPath+"
"
); //获取web项目的上下文路径 String strContextPath = request.getContextPath(); System.out.println("获取web项目的上下文路径"+strContextPath); out.print("获取web项目的上下文路径--request.getContextPath():"+strContextPath+"
"
); } public void init() throws ServletException { } }

浏览器显示:

request.getServletPath():/testPath

***request.getSession().getServletContext().getRealPath(request.getRequestURI())***
文件的绝对路径:D:\apache-tomcat-7.0.40\webapps\SSM-2\SSM-2\testPath

*** new File(request.getSession().getServletContext().getRealPath(request.getRequestURI())).getParent()***
目录的绝对路径:D:\apache-tomcat-7.0.40\webapps\SSM-2\SSM-2

获取web项目的全路径---getServletContext().getRealPath('/'):D:\apache-tomcat-7.0.40\webapps\SSM-2\

获取web项目的上下文路径--request.getContextPath():/SSM-2

console控制台输出:

request.getLocalAddr()--:127.0.0.1
request.getLocalName()--:bandicam.com
request.getLocalPort()--:8088
request.getLocales()--:java.util.Collections$2@4519778a
request.getRemoteAddr()--:127.0.0.1
request.getRemoteHost()--:127.0.0.1
request.getRemotePort()--:4829
request.getRequestedSessionId()--:B3FADDA338A2FA3D59BCCC46C7722C56
request.getRequestURI()--:/SSM-2/testPath
request.getRequestURL()--:http://localhost:8088/SSM-2/testPath
request.getServerName()--:localhost
request.getServerPort()--:8088
request.getServletPath()--:/testPath
request.getContextPath()--:/SSM-2
request.getContentLength()--:-1
request.getServletPath():/testPath
request.getSession().getServletContext().getResourcePaths('/')[/index.jsp, /UI_jsp/, /WEB-INF/, /UI_File/, /testPath1.jsp, /META-INF/]
request.getSession().getServletContext().getServletContextName():null
getServletConfig().getServletName():TestPath
getServletConfig().getServletContext().getContextPath():/SSM-2
文件的绝对路径:D:\apache-tomcat-7.0.40\webapps\SSM-2\SSM-2\testPath
目录的绝对路径:D:\apache-tomcat-7.0.40\webapps\SSM-2\SSM-2
获取web项目的全路径:D:\apache-tomcat-7.0.40\webapps\SSM-2\
获取web项目的上下文路径:/SSM-2

你可能感兴趣的:(jsp,servlet)