servlet——web应用中路径问题

target.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>target.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body>
    目标资源html页面
  </body>
</html>

servlet:

package path;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * web应用中路径问题
 * @author Administrator
 *
 */
public class PathDemo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html; charset=utf-8");
        //目标资源: target.html
        /*
         * 思考: 目标资源是给谁使用的。
         *         给服务器使用的:   / 表示在当前web应用的根目录(webRoot下)
         *         给浏览器使用的: /  表示在webapps的根目录下
         */

        /*
         * 1.转发
         *     给服务器使用的
         */
//        request.getRequestDispatcher("/target.html").forward(request, response);

        /*
         * 2.请求重定向
         *     给浏览器使用的
         */
//        response.sendRedirect("/day11/target.html");

        /*
         * 3.html页面的超连接href
         * 给浏览器使用的
         */
//        response.getWriter().write("<html><body><a href=‘/day11/target.html‘>超链接</a></body></html>");

        /*
         * 4.html页面中的form提交地址
         */
        response.getWriter().write("<html><body><form action=‘/day11/target.html‘><input type=‘submit‘/></form></body></html>");
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>

  <servlet>
    <servlet-name>ResourceDemo</servlet-name>
    <servlet-class>resource.ResourceDemo</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ResourceDemo</servlet-name>
    <url-pattern>/ResourceDemo</url-pattern>
  </servlet-mapping>
</web-app>
时间: 2024-08-07 18:25:46

servlet——web应用中路径问题的相关文章

Java Web 开发中路径相关问题小结

Java Web开发中路径问题小结 (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 Eclipse中目录结构如图2所示: 图2 那么针对这个站点的几个基本概念表述如下: 1. web站点的根目录:http://localhost:8080/ 2. web应用程序的的根目录:http://localhost:8080/test/ 3.同级目录:http://localhost:8080/test/articles/article1.jsp和http://

Java Web开发中路径问题小结

看以博客感觉不错,分享一下http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 Eclipse中目录结构如图2所示: 图2 那么针对这个站点的几个基本概念表述如下: 1. web站点的根目录:http://localhost:8080/ 2. web应用程序的的根目录:http://localhost:8080/test/ 3.同

JSP/Servlet Web应用中.properties文件的放置与读取

本地项目在本地类库中,我经常使用当前目录来放置.properties文件,这时调用方只要引用我的jar,并且将我的.properties放在他的classpath里面即可,比如:p.load(new FileInputStream("HanLP.properties"));这样类似下图的项目结构就可以跑起来:JSP/Servlet Web项目但是到了Web项目时,情况大不相同.在Tomcat下,classpath里只含apache-tomcat-8.0.14\bin目录下有限的...

获取SilverLight.Web项目中路径Uri

方法一: //获取指定要呈现的xaml内容的包活xaml文件Uri var strFullUrl = Application.Current.Host.Source.AbsoluteUri; if (strFullUrl.IndexOf("ClientBin") > 0) { var uristr = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + "Report/Default.

关于web开发中路径的问题的总结

web开发中的一个困扰web开发新人的是路径问题: 1:项目的静态资源的根路径:http://localhost:8080/sqec-monitor 即是部署在web服务器中(比如tomcat)中项目(假设此项目名为sqec-monitor)的webapp: 2:深入理解绝对路径和相对路径,以及相对路径中的" .." 和" . ":  3:前端给后端路径,而且都是完整的绝对路径,后端给前端文本: 4:一些引入项目静态资源的href,scr,url等等非常智能,能够自

servlet以及HTML中路径问题

路径问题: ①相对路径和绝对路径: 绝对路径:绝对路径是以/开头的路径! 相对于当前服务器的绝对路径:如果是服务器解析,那么/就代表当前服务器的绝对路径:http://localhost:8080 相对于当前web应用的绝对路径:代表的是http://localhost:8080/项目名/ 如果是服务器端解析,代表的是:http://localhost:8080/项目名/ 如果是浏览器端解析,代表的是:http://localhost:8080 相对路径:不是以/开头的路径是相对路径,相对的是当

Web应用的路径问题

1.总结:只有转发不用写项目的路径,其他都要! /** * web应用中路径问题 * @author APPle * */ public class PathDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text

web.xml中classpath 解释

经过我在对 web.xml 的配置测试: web.xml 中classpath 所指的路径是项目工程路径下的 classes 文件夹 然后在电脑中该路径就在这: 所以在web.xml中路径配置就是

解决servlet在web.xml中的路径跳转问题

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.s