页面跳转+路径问题

Posted 温一壶月光当茶饮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了页面跳转+路径问题相关的知识,希望对你有一定的参考价值。

 

 

 

  

绝对路径:是从盘符开始的路径,形如             // 多用在后台中
C:\windows\system32\cmd.exe
相对路径:是从当前路径开始的路径,假如当前路径为C:\windows                   //多用在前端的js  和

 

/开头的通常表示绝对路径:

/表示站点路径的场景如下(其他情况是在服务器上跳转  / 表示项目的根)

(1)  重定向

(2)    超链接

(3)    action表单

 

在web项目中,为了安全,可能需要把jsp文件放在WEB-INF目录下,这样如果我们的页面中出现超链接a标签或者js的location.href去直接转向到WEB-INF下的某一个jsp页面,那么就会引用不到,因为这样的请求方式是客户端的请求,而WEB-INF页面只对服务端开放,对客户端是不可见的。(只能通过转发的方式进入)

 

1  超链接  

<body>
	<a href="/tiaozhuan/views/index.jsp">超链接jsp跳转</a>   //进入jsp页面
	<a href="/tiaozhuan/servletA">超链接跳服务器(注解)</a>
	<a href="/tiaozhuan/servletB">超链接跳服务器(web.xml)</a>
</body>


(1)   方式1  通过注解进入servlet
@WebServlet("/servletA")
public class ChaoLianJieServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("doGet"+Thread.currentThread().getName());
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("doPost"+Thread.currentThread().getName());
		//doGet(req, resp);
	}

(2)  方式2  通过web.xml配置进入servlet
public class ChaoLianjieServlet1 extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("ChaoLianjieServlet1 +doPost");
		doGet(req, resp);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}	
}


web.xml


<servlet>
  	<servlet-name>servletB</servlet-name>
  	<servlet-class>com.rl.chaolianjie.ChaoLianjieServlet1</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>servletB</servlet-name>
  	<url-pattern>/servletB</url-pattern>
  </servlet-mapping>
  

 action :

 

<form  method="post" action="/tiaozhuan/servletB">

	<input type="submit" value="submit"/>
</form>



<servlet>
  	<servlet-name>servletB</servlet-name>
  	<servlet-class>com.rl.chaolianjie.ChaoLianjieServlet1</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>servletB</servlet-name>
  	<url-pattern>/servletB</url-pattern>
  </servlet-mapping>
  

public class ChaoLianjieServlet1 extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("ChaoLianjieServlet1 +doPost");
		//doGet(req, resp);
	}
}

重定向和转发:

 

<form  method="post" action="/tiaozhuan/servletA">

	<input type="submit" value="submit1"/>
</form>

<form  method="get" action="/tiaozhuan/servletA">

	<input type="submit" value="submit2"/>
</form>


@WebServlet("/servletA")
public class ChaoLianJieServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.sendRedirect("/tiaozhuan/views/index.jsp");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/views/index.jsp").forward(req, resp);
		
	}

  读取文件/获取初始化参数:

    1  配置

<servlet>
  	<servlet-name>servletB</servlet-name>
  	<servlet-class>com.rl.chaolianjie.ChaoLianjieServlet1</servlet-class>
  	<init-param>
  		<param-name>name</param-name>
  		<param-value>zhang</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>

  

 

    2   获取

        



      String path=getServletContext().getRealPath("/file/web.xml");    System.out.println(path); BufferedReader br=new BufferedReader( new FileReader(new File(path))); System.out.println(br.readLine()); //获取初始化参数 String name=getServletContext().getInitParameter("name"); String nameconf=getServletConfig().getInitParameter("name"); resp.getWriter().print(name+"getServletContext");//null resp.getWriter().print(nameconf+"getServletConfig");//有值

  

1.FileTest.class.getResource("")
  得到的是当前类FileTest.class文件的URI目录。不包括自己!

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/com/test/

  2.FileTest.class.getResource("/")
  得到的是当前的classpath的绝对URI路径。

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/

  3.Thread.currentThread().getContextClassLoader().getResource("")
  得到的也是当前ClassPath的绝对URI路径。

  如:file:/D:/java/eclipse32/workspace/jbpmtest3/bin/

  4.FileTest.class.getClassLoader().getResource("")
  得到的也是当前ClassPath的绝对URI路径。

 

服务器上:没加 /

System.out.println("bb"+Thread.currentThread().getContextClassLoader().getResource(""));
System.out.println("cc"+ChaoLianJieServlet.class.getResource(""));

 

bbfile:/D:/work/eclipse/workspaceOne/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/tiaozhuan/WEB-INF/classes/
ccfile:/D:/work/eclipse/workspaceOne/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/tiaozhuan/WEB-INF/classes/com/rl/chaolianjie/

 

服务器上 :  加 了/

System.out.println("aa"+Thread.currentThread().getContextClassLoader().getResource("/"));
System.out.println("dd"+ChaoLianJieServlet.class.getResource("/"));

 

aafile:/D:/work/eclipse/workspaceOne/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/tiaozhuan/WEB-INF/classes/
ddfile:/D:/work/eclipse/workspaceOne/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/tiaozhuan/WEB-INF/classes/

 

 

本地:

URL path=FileResourse.class.getResource("/");
System.out.println(path);     // file:/D:/work/eclipse/workspaceOne/tiaozhuan/build/classes/
URL path1=FileResourse.class.getResource("");
System.out.println(path1);    //file:/D:/work/eclipse/workspaceOne/tiaozhuan/build/classes/com/rl/chaolianjie/
URL a=Thread.currentThread().getContextClassLoader().getResource("");
System.out.println(a);          //file:/D:/work/eclipse/workspaceOne/tiaozhuan/build/classes/
URL b= Thread.currentThread().getContextClassLoader().getResource("/");
System.out.println(b);         null
URL c=Thread.currentThread().getClass().getResource("");
System.out.println(c);         null
URL path2 = Thread.currentThread().getContextClassLoader().getResource("/");
URL path3 = Thread.currentThread().getContextClassLoader().getResource("");
System.out.println(path2);      null
System.out.println(path3);     file:/D:/work/eclipse/workspaceOne/tiaozhuan/build/classes/

 

  服务器上:加了/  Thread.currentThread().getContextClassLoader().getResource("/")和当前类.class.getResource("/")的值都是classpath的绝对URI路径

       不加/  Thread.currentThread().getContextClassLoader().getResource("")得到的是classpath的绝对URI路径     而当前类.class.getResource("")的值是包结构下

 

  本地   加了/    当前类.class.getResource("/") 是classpath 下绝对URI    , Thread.currentThread().getContextClassLoader().getResource("/")是null

       不加/   当前类.class.getResource("")是当前孢结构下          T hread.currentThread().getContextClassLoader().getResource("")  是 classpath 下 绝对URI 路径

以上是关于页面跳转+路径问题的主要内容,如果未能解决你的问题,请参考以下文章

页面判断跳转路径

我要把页面上的图片由点击按钮存到手机相册指定路径里,js代码怎么写?(.net c#)

HTML代码片段

HTML代码片段

单页应用及多页应用

Struts2框架里面action与前端jsp页面进行交互路径问题---》一个对话框里面有很多超链接,进行相应的跳转