2014-08-04 23:21 2 people read Comments (0) Favorites Edit Delete
Directory (?) [+]
[-]
- Create a new servlet under the Srcmainjava
- Modify Webxml
- New JSP
- 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?
- <span style="font-weight:normal;" > Packagecom.deppon.text01.action;
- Import java.io.IOException;
- Import javax.servlet.ServletException;
- Import Javax.servlet.http.HttpServlet;
- Import Javax.servlet.http.HttpServletRequest;
- Import Javax.servlet.http.HttpServletResponse;
- Public class Userservlet extends HttpServlet {
- private Static final long serialversionuid = 1L;
- protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
- DoPost (request, response);
- }
- protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
- Request.setcharacterencoding ("UTF-8");
- Response.setcontenttype ("Text/html;charset=utf-8");
- String action = Request.getparameter ("action");
- if ("Login_input". Equals (Action)) {
- Request.getrequestdispatcher ("login.jsp"). Forward (request, response);
- } Else if ("Login". Equals (Action)) {
- String name = Request.getparameter ("name");
- String password = request.getparameter ("password");
- System.out.println ("name->" + name + ",password->" + password);
- }
- }
- }</span>
2. Modify Web. xml
[Java]View Plaincopyprint?
- <pre name="code" class="html" ><span style="font-weight:normal;" ><?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- 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 ">
- <servlet>
- <servlet-name>UserServlet</servlet-name>
- <servlet-class>com.deppon.text01.action.userservlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>UserServlet</servlet-name>
- <url-pattern>/user</url-pattern>
- </servlet-mapping>
- </web-app></span>
3. Create a new JSP
index.jsp
[Java]View Plaincopyprint?
- <span style="font-weight:normal;" ><%@ page language="java" contenttype="text/html; Charset=utf-8 "
- pageencoding="UTF-8"%>
- <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c5>
- <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
- <title>hello maven</title>
- <body>
- <p> Hello! </p>
- <a href="User?action=login_input" > go to login </a>
- </body>
login.jsp
[Java]View Plaincopyprint?
- <%@ page language="java" contenttype="text/html; Charset=utf-8 "
- pageencoding="UTF-8"%>
- <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c4>
- <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
- <title> Login Screen </title>
- <body>
- <form action="User?action=login" method="POST" >
- Name:<input type="text" name="name"/>
- Password:<input type="Password" name="Password"/>
- <input type="Submit" value="login"/>
- </form>
- </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?
- <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>
- <groupId>com.deppon.demo</groupId>
- <artifactid>test01</artifactid>
- <packaging>war</packaging>
- <version>0.0.1-snapshot</version>
- <name>test01 Maven Webapp</name>
- <URL>http://maven.apache.org</url>
- <!--property configuration --
- <properties>
- <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
- </Properties>
- <!--dependency configuration --
- <dependencies>
- <!--adding JUnit --
- <dependency>
- <groupId>junit</groupId>
- <artifactid>junit</artifactid>
- <version>3.8.1</version>
- <scope>test</scope>
- </Dependency>
- <!--adding servlets --
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactid>servlet-api</artifactid>
- <version>2.5</version>
- <scope>provided</scope>
- </Dependency>
- </Dependencies>
- <build>
- <finalname>test01 </finalname>