11. MAVEN publishes Web project

Source: Internet
Author: User

1. Create a new MAVEN project and select the WebApp framework

2. Write a servlet

Package Com.onyas.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.onyas.user.model.user;public class HelloServlet extends HttpServlet {/** *  */private static final long serialversionuid = 1L; @Overrideprotected void Doget ( HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {req.setattribute ("user", new User ("admin", "123", "Administrator"), Req.getrequestdispatcher ("/hello.jsp"). Forward (req, resp);}}

3, in Src/main/webapp/web-inf/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 "><servlet><servlet-name>helloservlet</ servlet-name><servlet-class>com.onyas.servlet.helloservlet</servlet-class></servlet>< servlet-mapping><servlet-name>helloservlet</servlet-name><url-pattern>/hello.do</ Url-pattern></servlet-mapping></web-app>

4, the new hello.jsp under the SRC/MAIN/WEBAP

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

5, modified because of the introduction of the user class, so to add the dependency on other modules,If you do not find the jar package that you wrote at this time, because you did not install your own module into the local repository, you need to execute the clean install in your own module pom.xml.

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" ><modelversion >4.0.0</modelVersion><parent><groupId>com.onyas.user</groupId><artifactId> User-parent</artifactid><version>0.0.1-snapshot</version><relativepath&gt, .... /user-parent/pom.xml</relativepath></parent><artifactid>user-web</artifactid>< Packaging>war</packaging><name>user-web Maven webapp</name><url>http:// maven.apache.org</url><dependencies><dependency><groupid>junit</groupid>< artifactid>junit</artifactid><version>4.10</version><scope>test</schttp:// localhost:8080/user-web/hello.doope></dependency><!--dependency on the servlet--><dependency>< Groupid>org.mortbay.jetty</groupid><artifactid>servlet-api</artifactid><version>2.5.20110712</version></dependency>< Dependency><groupid>${project.groupid}</groupid><artifactid>user-services</artifactid ><version>${project.version}</version></dependency></dependencies><build>< Finalname>user-web</finalname></build></project>

6. On Pom.xml, right-click Run as-->maven build. --Enter the clean package. The war package will be generated under the target package, copy the war package to Tomcat under the WebApp directory and start Tomcat.

You can see the results by entering http://localhost:8080/user-web/hello.do in the browser.


7. Do this after each modification to execute the clean package, then copy the war package, then run Tomcat, very troublesome, so now there is a plugin, search copy maven plugin can.



A better way to do this is to release it with jetty, first configuring the jetty plugin in the parent pom.xml:

<!--configuration Jetty plug-in  --><plugin><groupid>org.mortbay.jetty</groupid><artifactid> jetty-maven-plugin</artifactid><configuration><scanintervalseconds>10</ scanintervalseconds><webapp><contextpath>/hello</contextpath></webapp><!--Configuring the Listening port --><connectors><connector implementation= "Org.eclipse.jetty.server.nio.SelectChannelConnector" > <port>8082</port><maxIdleTime>60000</maxIdleTime></connector></connectors> </configuration></plugin>

Then add the jetty plugin to the Web project:

<build><finalName>user-web</finalName><plugins><!--Add Jetty plugin--><plugin> <groupid>org.mortbay.jetty</groupid><artifactid>jetty-maven-plugin</artifactid></ Plugin></plugins></build>

Right-click in the Pom.xml of the Web projectRun as-->maven Builder. ->clean Compile Jetty:run, you will see the console start jetty, and after the boot is complete, you can experiment in the browser


11. MAVEN publishes Web project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.