MAVEN's Learning data collection-(iv) building Web projects with maven-testing

Source: Internet
Author: User

2014-08-04 23:21 2 people read Comments (0) Favorites Edit Delete

Directory (?) [+]

[-]

    1. Create a new servlet under the Srcmainjava
    2. Modify Webxml
    3. New JSP
    4. Test

In our previous blog, we built a Web project using MAVEN, where we write a simple servlet and test it.

1. Under Src/main/java, create a new servlet [Java]View Plaincopyprint?
  1. <span style="font-weight:normal;"  > Packagecom.deppon.text01.action;
  2. Import java.io.IOException;
  3. Import javax.servlet.ServletException;
  4. Import Javax.servlet.http.HttpServlet;
  5. Import Javax.servlet.http.HttpServletRequest;
  6. Import Javax.servlet.http.HttpServletResponse;
  7. Public class Userservlet extends HttpServlet {
  8. private Static final long serialversionuid = 1L;
  9. protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
  10. DoPost (request, response);
  11. }
  12. protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
  13. Request.setcharacterencoding ("UTF-8");
  14. Response.setcontenttype ("Text/html;charset=utf-8");
  15. String action = Request.getparameter ("action");
  16. if ("Login_input". Equals (Action)) {
  17. Request.getrequestdispatcher ("login.jsp"). Forward (request, response);
  18. } Else if ("Login". Equals (Action)) {
  19. String name = Request.getparameter ("name");
  20. String password = request.getparameter ("password");
  21. System.out.println ("name->" + name + ",password->" + password);
  22. }
  23. }
  24. }</span>
2. Modify Web. xml [Java]View Plaincopyprint?
  1. <pre name="code" class="html" ><span style="font-weight:normal;" ><?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0"
  3. xmlns="Http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  5. Xsi:schemalocation= "http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
  7. <servlet>
  8. <servlet-name>UserServlet</servlet-name>
  9. <servlet-class>com.deppon.text01.action.userservlet</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>UserServlet</servlet-name>
  13. <url-pattern>/user</url-pattern>
  14. </servlet-mapping>
  15. </web-app></span>

3. Create a new JSP

index.jsp

[Java]View Plaincopyprint?
  1. <span style="font-weight:normal;" ><%@ page language="java" contenttype="text/html; Charset=utf-8 "
  2. pageencoding="UTF-8"%>
  3. <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c5>
  4. <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
  5. <title>hello maven</title>
  6. <body>
  7. <p> Hello! </p>
  8. <a href="User?action=login_input" > go to login </a>
  9. </body>

login.jsp
[Java]View Plaincopyprint?
  1. <%@ page language="java" contenttype="text/html; Charset=utf-8 "
  2. pageencoding="UTF-8"%>
  3. <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c4>
  4. <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
  5. <title> Login Screen </title>
  6. <body>
  7. <form action="User?action=login" method="POST" >
  8. Name:<input type="text" name="name"/>
  9. Password:<input type="Password" name="Password"/>
  10. <input type="Submit" value="login"/>
  11. </form>
  12. </body>

4. Testing

The project structure looks like this:

In fact, after the construction is completed, the development should be the same as the usual development of Web projects.

2013-04-28 Revision

Before forgetting to explain the Pom file, you need to add dependent:

Pom.xml

[HTML]View Plaincopyprint?
  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>com.deppon.demo</groupId>
  5. <artifactid>test01</artifactid>
  6. <packaging>war</packaging>
  7. <version>0.0.1-snapshot</version>
  8. <name>test01 Maven Webapp</name>
  9. <URL>http://maven.apache.org</url>
  10. <!--property configuration --
  11. <properties>
  12. <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
  13. </Properties>
  14. <!--dependency configuration --
  15. <dependencies>
  16. <!--adding JUnit --
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactid>junit</artifactid>
  20. <version>3.8.1</version>
  21. <scope>test</scope>
  22. </Dependency>
  23. <!--adding servlets --
  24. <dependency>
  25. <groupId>javax.servlet</groupId>
  26. <artifactid>servlet-api</artifactid>
  27. <version>2.5</version>
  28. <scope>provided</scope>
  29. </Dependency>
  30. </Dependencies>
  31. <build>
  32.     <finalname>test01 </finalname>  

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.