JSP路径解惑

简介: JSP路径解惑

项目结构:



 

webroot就是web项目发的tomcat服务器的根目录(myeclipse软件)


<%@ page language="java" import="java.util.*" contentType="text/html; charset=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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>Servlet路径跳转</h1>
<hr>




访问HelloServlet!

访问HelloServlet!

访问TestServlet,跳转到Test.jsp


 

web.xml中:


<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>



/servlet/HelloServlet

TestServlet


/servlet/TestServlet


index.jsp

 

------------------------------------------------------------------------

request 的常用方法:


request.getSchema()可以返回当前页面使用的协议,http 或是 https;


request.getServerName()可以返回当前页面所在的服务器的名字;


request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80;


request.getContextPath()可以返回当前页面所在的应用的名字;


下面的是针对在Eclispe或myeclipse中建立项目的例子而言的:


basePath比如为:http://localhost:8888/JSP_Path_Project/,

path则为:/JSP_Path_Project。


 


basePath:http://localhost:8888/JSP_Path/


getContextPath:/JSP_Path


getServletPath:/index.jsp


getRequestURI:/JSP_Path/index.jsp


getRequestURL:http://localhost:8888/JSP_Path/index.jsp


getRealPath:D:\eclipse examples\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\JSP_Path


getServletContext().getRealPath:D:\eclipse examples\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\JSP_Path

 

getContextPath():得到当前应用的根目录

 

 

 

getScheme():它返回当前请求所使用的协议。 一般的应用返回 "http",对于ssl则返回"https"

 

 

 

getServerName():获取服务器名字,如果是在本地的话就是localhost

 

 

 

getServerPort():获得服务器的端口号

相关文章
|
9月前
|
Java
jsp内置对象和路径
jsp内置对象和路径
63 0
jsp页面编程时的路径问题(绝对路径与相对路径)
浅谈在进行jsp页面编程时,路径问题的解决(绝对路径与相对路径)
|
Java
Servlet与JSP内置对象的对应关系、Servlet获取表单、Servlet路径跳转
Servlet与JSP内置对象的对应关系、Servlet获取表单、Servlet路径跳转
107 0
|
Java 应用服务中间件
JSP中图片路径包含中文的问题
JSP中图片路径包含中文的问题
127 0
|
Java 应用服务中间件
|
JavaScript Java 前端开发
JSP中servlet中的路径配置问题
在每个JavaWeb应用中,都有一个web.xml配置文件,该文件中配置了该web应用中的servlet的映射关系。
1088 0
|
Java 应用服务中间件
JAVA,JSP,Servlet获取当前工程路径-绝对路径
在jsp和class文件中调用的相对路径不同。 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。
1060 0

相关课程

更多