Java web开发之Tomcat目录和web工程简介

2017-11-07  本文已影响19人  flemingchen

Tomcat目录简介

tomcat目录

端口port可以按需要更改,如8888,80等等,增加URIEncoding属性为UTF-8,是为了在服务器处理get请求时,参数可能存在中文时避免乱码。


port

web工程简介

web工程

基本的web工程包括index.jsp和WEB-INF,WEB-INF里面包括classes和lib,最关键的是web.xml,classes里面包含的是工程编译生成的字节码文件.class,lib里是用到的库,web.xml里配置了程序的相关参数。

<%@ page language="java" import="java.util.*" 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">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>my first web app</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>你好,中国</h1>
    <hr>
  </body>
</html>
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.xhtml</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

将项目放到webapps目录下,启动tomcat后,运行http://localhost:8080/myfirstweb

运行结果

注意

真正完整的项目还会包括css,js,img等等这些资料,值得一说的是,这里的WEB-INF目录是客户端,也就是浏览器无法访问到的,只有服务器可以操作。

上一篇 下一篇

猜你喜欢

热点阅读