• web应用


    实现HttpServlet的Web应用

          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
              <version>3.1.0</version>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>javax.servlet.jsp</groupId>
              <artifactId>javax.servlet.jsp-api</artifactId>
              <version>2.2.1</version>
              <scope>provided</scope>
          </dependency>
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
          </dependency>
          
          <plugin>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>jetty-maven-plugin</artifactId>
              <version>8.1.16.v20140903</version>
              <configuration>
                  <scanIntervalSeconds>10</scanIntervalSeconds>
                  <connectors>
                      <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                          <port>80</port>
                      </connector>
                  </connectors>
                  <webAppConfig>
                      <contextPath>/</contextPath>
                  </webAppConfig>
              </configuration>
          </plugin>
    pom.xml
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/hello")
    public class MyServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setCharacterEncoding("utf-8");
            resp.getWriter().write("Hello World!");
        }
    
        public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    MyServlet

     使用SpringMVC的Web应用

        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.3.14.RELEASE</version>
        </dependency>
    
    
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.16.v20140903</version>
            <configuration>
              <scanIntervalSeconds>10</scanIntervalSeconds>
              <connectors>
                <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                  <port>80</port>
                </connector>
              </connectors>
              <webAppConfig>
                <contextPath>/</contextPath>
              </webAppConfig>
            </configuration>
          </plugin>
    pom.cml
    <?xml version="1.1" encoding="UTF-8"?>
    <web-app 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_3_0.xsd"
             version="3.0">
    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:my_spring_mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    </web-app>
    web.xml
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- Spring MVC提供的一键式配置方法,配置后Spring MVC会自动注册组件,比如映射器处理器、视图解析器等 -->
    <mvc:annotation-driven/>
    <!-- 包扫描 -->
    <!--<context:component-scan base-package="com.test"/>-->
    <!-- 包扫描,使用include-filter子标签设置只扫描@Controller,别的交给Spring容器去管理 -->
    <context:component-scan base-package="com" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    </beans>
    package com.test;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class MyController {
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String index(Model model) {
    
            model.addAttribute("msg", "Hello!");
            // 如果没有配置视图解析器ViewResolver,SpringMVC将默认使用
            // org.springframework.web.servlet.view.InternalResourceViewResolver作为ViewResolver,
            // 并且prefix和suffix都为空
            return "/WEB-INF/my.jsp";
        }
    }
    MyController
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>My JSP</title>
    </head>
    <body>
    <p>${msg}</p>
    </body>
    </html>
    my.jsp
  • 相关阅读:
    POJ 2195 Going Home(费用流)
    HDU 2485 Destroying the bus stations(费用流)
    POJ 3281 Dining(最大流)
    POJ 1122 FDNY to the Rescue!(最短路+路径输出)
    HDU 4747 Mex(线段树)
    POJ 2337 Catenyms
    UVa 10328 Coin Toss(Java大数+递推)
    HDU 1811 Rank of Tetris(拓扑排序+并查集)
    ZOJ 3747 Attack on Titans
    UVa 11404 回文子序列(LCS求最长回文串长度)
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/10351983.html
Copyright © 2020-2023  润新知