package com.servlet;
import java.io.IOException;
import javax.jws.WebService;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
/*@WebServlet("/demo1")*/
/*@WebServlet(urlPatterns = "/demo1",name = "demo1",
initParams={@WebInitParam(name="user",value="root"),
@webInitParam(name="pwd",value="root")},loadOnStaryup=1)*/
public class ServletDemo1 implements Servlet{
public void destroy() {
System.out.println("这是ServletDemo1的destroy销毁!");
}
public ServletConfig getServletConfig() {
System.out.println("这是ServletDemo1的getServletConfig配置!");
return null;
}
public String getServletInfo() {
System.out.println("这是ServletDemo1的getServletInfo信息!");
return null;
}
public void init(ServletConfig config) throws ServletException {
System.out.println("这是ServletDemo1的init初始化!");
String user = config.getInitParameter("user");
System.out.println(user);
String pwd = config.getInitParameter("pwd");
System.out.println(pwd);
}
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
System.out.println("这是ServletDemo1的service服务!");
}
}
用户爱好注册
reg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'reg.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- form表单中默认的提交方式为get -->
<form action="demo1" method="post">
用户名:<input type="text" name = "name"><br>
密码:<input type="password" name = "pwd"><br>
爱好:<input type="checkbox" name = "hobbys" value="shopping">shopping
<input type="checkbox" name = "hobbys" value="play">play
<input type="checkbox" name = "hobbys" value="读书">读书<br>
<input type="submit" value="注册"><input type = "reset" value="重置">
</form>
</body>
</html>
Demo1
package com.servlet;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/demo1")
public class Demo1 extends HttpServlet{
/*protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
form表单中默认的提交方式为get get使用servlet中的编码格式
* 方法1、<form action="demo1" method="get">
* 统一设置编码格式 方法2、 <form action="demo1" method="post">
* 调用request.setCharacterEncoding("UTF-8")*/
//request.getParameter("name")获取form表单中name属性对应名称的value值
/*String name = request.getParameter("name");
System.out.println(name);
String pwd = request.getParameter("pwd");
System.out.println(pwd);
String sex = request.getParameter("sex");
System.out.println(sex);
String hobbys = request.getParameter("hobbys");
System.out.println(hobbys);
String[] hobbys = request.getParameterValues("hobbys");
for (String hobby : hobbys) {
System.out.println(hobby);
}
}*/
//get请求和post请求的区别 post请求地址栏更安全
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("get请求");
String name = request.getParameter("name");
System.out.println(name);
String pwd = request.getParameter("pwd");
System.out.println(pwd);
}@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("post请求");
String name = request.getParameter("name");
System.out.println(name);
String pwd = request.getParameter("pwd");
System.out.println(pwd);
//getParameterMap()获取所有的请求参数,并以Map集合方式返回
Map<String, String[]> map = request.getParameterMap();
Set<Entry<String, String[]>> set = map.entrySet();
for (Entry<String, String[]> entry : set) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}