javaservlet POST示例

时间:2020-02-23 14:37:15  来源:igfitidea点击:

此示例演示如何使用Servlet的doPost()方法处理POST请求

在前面的Javaservlet教程示例中,我演示了doGet()方法的用法。现在,将介绍如何使用doPost()方法处理表单提交。javaservlets可以处理各种类型的请求。下面的列表显示了所有方法及其用途

项目结构

在我们的项目中,我们确实需要三个文件。pom.xml文件–设置Maven依赖项和构建属性,web.xml文件–将Servlet和Servlet本身配置为java类

Servlet POST示例项目结构

马文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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>net.theitroad.tutorials</groupId>
	<artifactId>ServletPOSTExample</artifactId>
	<version>1</version>
	<packaging>war</packaging>

	<name>Servlet POST Example</name>
	<url>https://theitroad.local</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>servletpost</finalName>
      <sourceDirectory>src/main/java</sourceDirectory>

      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-war-plugin</artifactId>
              <version>2.3</version>
              <configuration>
                  <warSourceDirectory>src/main/webapp</warSourceDirectory>
              </configuration>
          </plugin>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
      </plugins>
  </build>
</project>

与上一个教程一样,我们使用依赖关系

javax.servlet-api

对于我们的Servlet和

maven-war-plugin

构建web应用程序

将Servlet映射到web.xml文件文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<display-name>Simple Servlet Application</display-name>

	<servlet>
		<servlet-name>servletPost</servlet-name>
		<servlet-class>net.theitroad.tutorials.ServletPOST</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>servletPost</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>

</web-app>

servlet被命名为“servletPost”,它指向java类servletPost

在servlet映射中,我们将url“/welcome”分配给servlet

Servlet类

package net.theitroad.tutorials;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletPOST extends HttpServlet {

	private static final long serialVersionUID = -1641096228274971485L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		//set response headers
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");

		//create HTML form
		PrintWriter writer = response.getWriter();
		writer.append("<!DOCTYPE html>\r\n")
			  .append("<html>\r\n")
			  .append("		<head>\r\n")
			  .append("			<title>Form input</title>\r\n")
			  .append("		</head>\r\n")
			  .append("		<body>\r\n")
			  .append("			<form action=\"welcome\" method=\"POST\">\r\n")
			  .append("				Enter your name: \r\n")
			  .append("				<input type=\"text\" name=\"user\" \r\n")
			  .append("				<input type=\"submit\" value=\"Submit\" \r\n")
			  .append("			</form>\r\n")
			  .append("		</body>\r\n")
			  .append("</html>\r\n");
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String user = request.getParameter("user");

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");

		//create HTML response
		PrintWriter writer = response.getWriter();
		writer.append("<!DOCTYPE html>\r\n")
			  .append("<html>\r\n")
			  .append("		<head>\r\n")
			  .append("			<title>Welcome message</title>\r\n")
			  .append("		</head>\r\n")
			  .append("		<body>\r\n");
		if (user != null && !user.trim().isEmpty()) {
			writer.append("	Welcome " + user + ".\r\n");
			writer.append("	You successfully completed this theitroad.local example.\r\n");
		} else {
			writer.append("	You did not entered a name!\r\n");
		}
		writer.append("		</body>\r\n")
			  .append("</html>\r\n");
	}	

}

在上面的代码中, doGet()方法用于显示表单。客户端(浏览器)调用urlhttp://yoururl.com:8080/servletpost/welcome with GET request它看到下面的表格

输入表单

servlet在客户端浏览器中呈现为HTML,如下所示:

<!DOCTYPE html>
<html>
		<head>
			<title>Form input</title>
		</head>
		<body>
			<form action="welcome" method="POST">
				Enter your name: 
				<input type="text" name="user" 
				<input type="submit" value="Submit" 
			</form>
		</body>
</html>

提交后,调用servlet中的 doPost()方法。其中我们根据用户的输入构建一个响应。如果名称字段填写正确,则用户将收到一条问候消息