码迷,mamicode.com
首页 > 编程语言 > 详细

第一个JavaWeb工程

时间:2015-12-15 21:13:46      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

  这个工程主要用来研究log4j,所以就只有一个页面,希望以后慢慢进步。

  java动态生成网页主要使用servlet。把请求拦截下来,处理后返回结果。


这里创建的是一个maven工程。 结构如下:

技术分享

因为是部署在tomcat容器中,所以需要配置 web.xml,主要处理逻辑在HelloWorldController.java,spring-context-config.xml 里面是spring的配置,主要是开包扫描加载controller的注解。


 

下面是例行的贴代码:

技术分享
  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>me.demo</groupId>
  5     <artifactId>test-web-app</artifactId>
  6     <packaging>war</packaging>
  7     <version>1.0-SNAPSHOT</version>
  8     <name>TestWebApp</name>
  9     <url>http://maven.apache.org</url>
 10     <dependencies>
 11         <dependency>
 12             <groupId>junit</groupId>
 13             <artifactId>junit</artifactId>
 14             <version>4.8.2</version>
 15             <scope>test</scope>
 16         </dependency>
 17 
 18 
 19         <dependency>
 20             <groupId>org.springframework</groupId>
 21             <artifactId>spring-core</artifactId>
 22             <version>4.1.4.RELEASE</version>
 23         </dependency>
 24         <!-- 添加spring-context包 -->
 25         <dependency>
 26             <groupId>org.springframework</groupId>
 27             <artifactId>spring-context</artifactId>
 28             <version>4.1.4.RELEASE</version>
 29         </dependency>
 30         <!-- 添加spring-tx包 -->
 31         <dependency>
 32             <groupId>org.springframework</groupId>
 33             <artifactId>spring-tx</artifactId>
 34             <version>4.1.4.RELEASE</version>
 35         </dependency>
 36         <!-- 添加spring-jdbc包 -->
 37         <dependency>
 38             <groupId>org.springframework</groupId>
 39             <artifactId>spring-jdbc</artifactId>
 40             <version>4.1.4.RELEASE</version>
 41         </dependency>
 42         <!-- 为了方便进行单元测试,添加spring-test包 -->
 43         <dependency>
 44             <groupId>org.springframework</groupId>
 45             <artifactId>spring-test</artifactId>
 46             <version>4.1.4.RELEASE</version>
 47         </dependency>
 48         <!--添加spring-web包 -->
 49         <dependency>
 50             <groupId>org.springframework</groupId>
 51             <artifactId>spring-web</artifactId>
 52             <version>4.1.4.RELEASE</version>
 53         </dependency>
 54         <!--添加aspectjweaver包 -->
 55         <dependency>
 56             <groupId>org.aspectj</groupId>
 57             <artifactId>aspectjweaver</artifactId>
 58             <version>1.8.5</version>
 59         </dependency>
 60         <!-- 添加mybatis的核心包 -->
 61         <dependency>
 62             <groupId>org.mybatis</groupId>
 63             <artifactId>mybatis</artifactId>
 64             <version>3.2.8</version>
 65         </dependency>
 66         <!-- 添加mybatis与Spring整合的核心包 -->
 67         <dependency>
 68             <groupId>org.mybatis</groupId>
 69             <artifactId>mybatis-spring</artifactId>
 70             <version>1.2.2</version>
 71         </dependency>
 72         <!-- 添加servlet3.0核心包 -->
 73         <dependency>
 74             <groupId>javax.servlet</groupId>
 75             <artifactId>javax.servlet-api</artifactId>
 76             <version>3.0.1</version>
 77         </dependency>
 78         <dependency>
 79             <groupId>javax.servlet.jsp</groupId>
 80             <artifactId>javax.servlet.jsp-api</artifactId>
 81             <version>2.3.2-b01</version>
 82         </dependency>
 83         <!-- jstl -->
 84         <dependency>
 85             <groupId>javax.servlet</groupId>
 86             <artifactId>jstl</artifactId>
 87             <version>1.2</version>
 88         </dependency>
 89         <!-- ODBC14 -->
 90         <dependency>
 91             <groupId>com.oracle</groupId>
 92             <artifactId>ojdbc14</artifactId>
 93             <version>10.2.0.1.0</version>
 94         </dependency>
 95         <!-- DBCP包 -->
 96         <dependency>
 97             <groupId>commons-dbcp</groupId>
 98             <artifactId>commons-dbcp</artifactId>
 99             <version>1.4</version>
100         </dependency>
101         <!-- 添加druid连接池包 -->
102         <dependency>
103             <groupId>com.alibaba</groupId>
104             <artifactId>druid</artifactId>
105             <version>1.0.12</version>
106         </dependency>
107 
108         <dependency>
109             <groupId>log4j</groupId>
110             <artifactId>log4j</artifactId>
111             <version>1.2.9</version>
112         </dependency>
113         <dependency>
114             <groupId>org.apache.maven.plugins</groupId>
115             <artifactId>maven-resources-plugin</artifactId>
116             <version>2.4.3</version>
117         </dependency>
118     </dependencies>
119     <build>
120         <finalName>TestWebApp</finalName>
121         <plugins>
122             <plugin>
123                 <groupId>org.apache.maven.plugins</groupId>
124                 <artifactId>maven-compiler-plugin</artifactId>
125                 <configuration>
126                     <source>1.6</source>
127                     <target>1.6</target>
128                 </configuration>
129             </plugin>
130         </plugins>
131     </build>
132 </project>
pom.xml
技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 5             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 6          version="3.0">
 7     <!-- icon -->
 8     <!-- display-name -->
 9     <display-name>TestWebApp</display-name>
10     <!-- context-param -->
11     <context-param>
12         <param-name>contextConfigLocation</param-name>
13         <param-value>classpath*:spring-context-config.xml</param-value>
14     </context-param>
15     <!-- filter -->
16     <!--<servlet>-->
17         <!--<servlet-name>HelloWorld</servlet-name>-->
18         <!--<servlet-class>me.demo.controller.HelloWorldController</servlet-class>-->
19     <!--</servlet>-->
20     <!--<servlet-mapping>-->
21         <!--<servlet-name>HelloWorld</servlet-name>-->
22         <!--<url-pattern>/HelloWorld</url-pattern>-->
23     <!--</servlet-mapping>-->
24     <filter>
25         <filter-name>encodingFilter</filter-name>
26         <filter-class>
27             org.springframework.web.filter.CharacterEncodingFilter
28         </filter-class>
29         <init-param>
30             <param-name>encoding</param-name>
31             <param-value>utf-8</param-value>
32         </init-param>
33     </filter>
34     <!-- filter-mapping -->
35     <filter-mapping>
36         <filter-name>encodingFilter</filter-name>
37         <url-pattern>/*</url-pattern>
38     </filter-mapping>
39     <!-- listener servlet -->
40     <listener>
41         <description>Spring监听器</description>
42         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
43     </listener>
44     <!-- servlet -->
45     <!-- servlet-mapping -->
46     <!-- session-config -->
47     <!-- mime-mapping -->
48     <!-- welcom-file-list -->
49     <welcome-file-list>
50         <welcome-file>HelloWorld</welcome-file>
51     </welcome-file-list>
52     <!-- error-page -->
53     <!-- taglib -->
54     <!-- resource-env-ref -->
55     <!-- resource-ref -->
56     <!-- security-constraint -->
57     <!-- login-config -->
58     <!-- security-role-->
59     <!-- env-entry -->
60     <!-- ejb-ref -->
61     <!-- ejb-local-ref -->
62 </web-app>
web.xml
技术分享
 1 package me.demo.controller;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 
13 /**
14  * Created by bl05973 on 2015/12/15.
15  */
16 @WebServlet("/HelloWorld")
17 public class HelloWorldController extends HttpServlet {
18     Log logger = LogFactory.getLog(HttpServlet.class);
19 
20     public void run(String param) {
21         System.out.println("[Sysout]"+param);
22         logger.info("[run] param is" + param);
23     }
24 
25     public void run() {
26         run("null");
27     }
28 
29     public void doGet(HttpServletRequest request, HttpServletResponse response)
30             throws ServletException, IOException {
31         run();
32         request.getRequestDispatcher("/HelloWorld.jsp").forward(request, response);
33     }
34     public void doPost(HttpServletRequest request, HttpServletResponse response)
35             throws ServletException, IOException {
36         String param = request.getParameter("param");
37         run(param);
38         request.getRequestDispatcher("/HelloWorld.jsp").forward(request, response);
39     }
40 
41     public void init() {
42         logger.info("init ----------------------------------------------------------------------");
43     }
44 }
HelloWorldController.java
技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6               http://www.springframework.org/schema/beans/spring-beans.xsd
 7               http://www.springframework.org/schema/context
 8               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9        <!-- 对 me.demo.service 的包扫描 -->
10        <context:component-scan base-package="me.demo.service" />
11 </beans>
spring-context-config.xml
技术分享
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: bl05973
 4   Date: 2015/12/15
 5   Time: 15:19
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>HelloWorld</title>
12   <script type="text/javascript">
13     <!--
14     function jump(target) {
15       window.location.href = target;
16     }
17     -->
18   </script>
19 </head>
20 <body>
21   Hello World!
22   <form method="post" target="/HelloWorld" >
23     <input name="param" type="text" />
24     <input type="submit">
25   </form>
26 </body>
27 </html>
HelloWorld.jsp

运行的主要流程就是HelloWorldController 里面重写了 doGet 和 doPost 方法,tomcat拿到 /HelloWorld 的请求会交给这个controller来处理。然后他处理完后台逻辑,会在最后一句的时候加载 HelloWorld.jsp模板,然后展示出来。

第一个JavaWeb工程

标签:

原文地址:http://www.cnblogs.com/Phantom01/p/5049411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!