Servlet form data to obtain detailed form

A. How to use Servlet

After the first javaweb created a project, create a servlet file in the root directory of Java Resources src in:

After you create the file in the code useless except for the following codes are deleted, leaving only the following code:

package com.jd.servlet;

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 LogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
}

 Select Servers in the console table, click on the blue color of this prompt:

 

Select the Tomcat version used in the example is Tomcat v8.0, and choose the path of Tomcat:

Then right-click on the prompt, click Add and Remove:

 

Select the class logSystem want to add, click Add to add:

 

At this point they added successfully, you can use the Servlet form data acquisition form:

II. Get the text form of the form

If the file is created in the jsp in Javaweb, wrote the following code:

<%@ 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">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Insert title here</title>
	</head>
	<body>
		<form action="./LogServlet">
			<input type="text" placeholder="请输入用户名" name="user-name" id="user-name"/>
			<input type="password" placeholder="请输入密码" name="password" id="password"/>
			<input type="submit" />
		</form>
	</body>
</html>

The resulting page in the browser as follows:

 

Then when the user enters data in text box and password box, in order to transfer data from the browser back to the code, you can write the following code in the class just created LogServlet:

The first parameter of the request doGet method is stored in log files form the form the value of each label to obtain in the page, so the call with a request getParameter () method and enter the label you want to get in the parameter list name value, a first exemplary input "user-name" name is the value of the text box, a second input of the "password" name value is the password box, in which case variables userName and password is stored acquired from the data page, a string, and then prints it on the console:

package com.jd.servlet;

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 LogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName;
		String password;
		userName=request.getParameter("user-name");
		password=request.getParameter("password");
		System.out.println(userName+":"+password);
	}
}

The following are the results after click submit:

 

 

Text fields, drop-down lists, radio buttons also get the principle of this, the following code sample:

log.jsp:

<%@ 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">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Insert title here</title>
	</head>
	<body>
		<form action="./LoginServlet">
			<input placeholder="请输入用户名" name="user_name"/>
			<input type="password" name="password" placeholder="请输入密码"/>
			<textarea name="summary" placeholder="请输入简介"></textarea>
			<input type="radio" name="sex" value="1"/>男<input type="radio" name="sex" value="2"/>女
			<select name="grade">
				<option>--请选择--<option/>
				<option>--一年级--<option/>
				<option>--二年级--<option/>
			</select>
			<input type="submit" value="登录"/>
		</form>
	</body>
</html>

 LogServlet.java:

package com.jd.servlet;

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 LogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName = request.getParameter("user_name");//文本框、密码框、文本域、下拉列表、单选框
		System.out.println(userName);
		String password = request.getParameter("password");
		System.out.println(password);
		String summary = request.getParameter("summary");
		System.out.println(summary);
		String grade = request.getParameter("grade");
		System.out.println(grade);
		String sex = request.getParameter("sex");
		System.out.println(sex);
	}

}

The following are input in the browser, click on the login form is submitted:

 The following is the output:

III. Gets the form of box form

Acquiring data similar to the radio frame, with the only difference that the call request object getParameter () method to get radio button is obtained when a variable of type String, the acquired data acquired box is an array of type String , acquired data sample code is as follows:

log.jsp:

<%@ 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">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Insert title here</title>
	</head>
	<body>
		<form action="./LogServlet">
			<!--<input placeholder="请输入用户名" name="user_name"/>
			<input type="password" name="password" placeholder="请输入密码"/>
			<textarea name="summary" placeholder="请输入简介"></textarea>
			<input type="radio" name="sex" value="1"/>男<input type="radio" name="sex" value="2"/>女
			<select name="grade">
				<option>--请选择--<option/>
				<option>--一年级--<option/>
				<option>--二年级--<option/>
			</select>-->
			<input type="checkbox" name="hobby" value="1"/>篮球<input type="checkbox" name="hobby" value="2"/>足球<input type="checkbox" name="hobby" value="3"/>乒乓球
			<input type="submit" value="登录"/>
		</form>
	</body>
</html>

LogServlet.java:

package com.jd.servlet;

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 LogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String userName = request.getParameter("user_name");//文本框、密码框、文本域、下拉列表、单选框
//		System.out.println(userName);
//		String password = request.getParameter("password");
//		System.out.println(password);
//		String summary = request.getParameter("summary");
//		System.out.println(summary);
//		String grade = request.getParameter("grade");
//		System.out.println(grade);
//		String sex = request.getParameter("sex");
//		System.out.println(sex);
		String [] hobbies = request.getParameterValues("hobby");
		for(String hobby:hobbies) {
			System.out.println(hobby);
		}
	}
}

在复选框中选择选项,点击登录提交表单: 

得到如下运行结果:

发布了99 篇原创文章 · 获赞 93 · 访问量 5227

Guess you like

Origin blog.csdn.net/DangerousMc/article/details/102864433