Servlet表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单get方式传值</title>
</head>
<body>
<fieldset id="get_code">
<legend>GET方式提交表单</legend>
<form action="${pageContext.request.contextPath}/form.action" method="get">
<input type="text" id="account" name="account" value="" autocomplete="off" placeholder="账号" />
<input type="password" id="password" name="password" value="" autocomplete="off" placeholder="密码" />
<br>
性别:
<label for="male">男</label>
<input type="radio" name="gender" value="1" id="male">
<label for="female">女</label>
<input type="radio" name="gender" value="0" id="female">
<br>
爱好:
<label for="swimming">踢足球</label>
<input type="checkbox" name="hobby" id="swimming" value="swimming" />
<label for="running">跑步</label>
<input type="checkbox" name="hobby" id="running" value="running" />
<label for="skiing">打篮球</label>
<input type="checkbox" name="hobby" id="skiing" value="skiing" />
<br>
籍贯:
<select name="hometown" id="hometown">
<option value="22">深圳</option>
<option value="11">北京</option>
<option value="00">上海</option>
</select>
<br>
水果:
<br>
<select name="fruits" multiple="multiple">
<option value="banana">香蕉</option>
<option value="apple">苹果</option>
<option value="pear">西瓜</option>
</select>
<br>
<input type="submit" id="get_btn" value="GET方式提交" />
</form>
</fieldset>
</body>
</html>
Servlet post表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单post方式传值</title>
</head>
<body>
<fieldset id="post_code">
<legend>POST方式提交表单</legend>
<form action="${pageContext.request.contextPath}/form.action" method="post">
<input type="text" id="account" name="account" value="" autocomplete="off" placeholder="账号" />
<input type="password" id="password" name="password" value="" autocomplete="off" placeholder="密码" />
<br>
性别:
<label for="male">男</label>
<input type="radio" name="gender" value="1" id="male">
<label for="female">女</label>
<input type="radio" name="gender" value="0" id="female">
<br>
爱好:
<label for="swimming">踢足球</label>
<input type="checkbox" name="hobby" id="swimming" value="swimming" />
<label for="running">跑步</label>
<input type="checkbox" name="hobby" id="running" value="running" />
<label for="skiing">打篮球</label>
<input type="checkbox" name="hobby" id="skiing" value="skiing" />
<br>
籍贯:
<select name="hometown" id="hometown">
<option value="22">吉林</option>
<option value="11">北京</option>
<option value="00">大海</option>
</select>
<br>
水果:
<br>
<select name="fruits" multiple="multiple">
<option value="banana">香蕉</option>
<option value="apple">苹果</option>
<option value="pear">西瓜</option>
</select>
<br>
<input type="submit" id="post_btn" value="POST方式提交" />
</form>
</fieldset>
</body>
</html>
Servlet上传文件
public class ServletPart extends HttpServlet {
private static final long serialVersionUID = 7755467677622542254L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getContentType());
if (request.getContentType().contains("multipart/form-data")) {
// 如果是否Multipart类型表单获取表单中上传的文件
// 获取Part对象
Part part = request.getPart("headSculpture");
//调用获取文件名的方法
String fileName = part.getSubmittedFileName();
System.out.println("源文件名>>>" + fileName);
// 获取文件后缀名
String extension = FilenameUtils.getExtension(fileName);
// 获取项目的绝对路径
String realPath = this.getServletContext().getRealPath("/");
// 上次文件默认目录
String upload = "static/upload";
String timePath = BaseLocalDateTime.nowString("yyyy/MM/dd/HH/mm");
// 上传目录
String upPath = realPath + File.separator + upload + File.separator + timePath;
File file = new File(upPath);
if (!file.exists()) {
file.mkdirs();
}
// 新文件名字
String partFileName = IdUtil.fastSimpleUUID() + "." + extension;
// Part 保存文件
part.write(upPath + File.separator + partFileName);
String protocol = "http://";
String localAddr = request.getLocalAddr();
int localPort = request.getLocalPort();
String contextPath = request.getContextPath();
String url = protocol + localAddr + ":" + localPort + contextPath;
System.out.println("Part 方式保存文件 >>> " + upPath + File.separator + partFileName);
System.out.println("Part 方式保存文件 >>> " + url + "/" + upload + "/" + timePath + "/" + partFileName);
} else {
System.out.println("表单应该配置multiple enctype=\"multipart/form-data\"");
}
}
}
Servlet接收json值
@WebServlet(urlPatterns = {"json.action"})
public class Json extends HttpServlet {
private static final long serialVersionUID = -7081278172438985849L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletInputStream inputStream = request.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String text = bufferedReader.readLine();
System.out.println(text);
response.setContentType("application/json;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print(text);
writer.flush();
writer.close();
}
}