(1)ServletContext作用域
可以写一个SetAttributeServlet文件与GetAttributeServlet
代码如下:
@WebServlet("/MyFirstServlet")
public class SetAttributeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//在ServletContext中存储一个信息
ServletContext servletContext=getServletContext();
//添加String类型到servletContext的作用域中
//第一种方式
servletContext.setAttribute("name", "zhangsan");
servletContext.setAttribute("sex", "nan");
//第二种方式,添加对象
servletContext.setAttribute("user", new User("wangwu","231"));
//移除,写上这行代码后就会移除掉这个对象
//servletContext.removeAttribute("User");
//第三种方式,添加集合
ArrayList<User> arrayList=new ArrayList<User>();
arrayList.add(new User("zhaoxin","1234"));
arrayList.add(new User("liubei","123435"));
servletContext.setAttribute("userList", arrayList);
}
/**
* 处理post请求
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用doGet
doGet(request, response);
}
}
GetAttributeServlet中的代码:
public class GetAttributeServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取servletContext的对象
ServletContext servletContext=getServletContext();
//获取普通参数
String name=(String)servletContext.getAttribute("name");
String sex=(String)servletContext.getAttribute("sex");
System.out.println(name+"---"+sex);
//获取对象
User user=(User)servletContext.getAttribute("user");
System.out.println(user.toString());//转换成string类型
//获取集合
ArrayList<User> userList=(ArrayList<User>)servletContext.getAttribute("userList");
for(User user2: userList) {
System.out.println(user2.toString());
}
}
}
(2)获取请求参数的几种方式
在RequestServlet.java中代码:
/**
* Servlet implementation class RequestServlet
*/
@WebServlet("/RequestServlet")
public class RequestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RequestServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求头信息
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String key=headerNames.nextElement();
//请求头的信息
String value=request.getHeader(key);
System.out.println(key+"-----"+value);
}
/**
* 获取请求方法
*/
String method=request.getMethod();
System.out.println(method);
/**
* 获取具体单个请求参数
*/
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+"----"+password);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在login.html中的代码样式如下:action中表明在提交表单时会跳转到RequestServlet页面,在这里的提交方法是get方法
运行结果:
点击登录后,会在后台console中显示出用户名与密码。
如下的HTML在点击注册时也会电泳RequestServlet文件,
我们也可以在RequestServlet中写代码来获取信息
/**
* Servlet implementation class RequestServlet
*/
@WebServlet("/RequestServlet")
public class RequestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RequestServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求头信息
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String key=headerNames.nextElement();
//请求头的信息
String value=request.getHeader(key);
System.out.println(key+"-----"+value);
}
/**
* 获取请求方法
*/
String method=request.getMethod();
System.out.println(method);
/**
* 获取请求参数
*/
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+"----"+password);
/**
* 在form.html中的爱好有多个,所以用数组
*/
String[] hobbys=request.getParameterValues("hobbys");
for(String h:hobbys) {
System.out.println(h);
}
//获取参数的map集合
Map<String,String[]> parameterMap=request.getParameterMap();
//获取到的是键值对的关系,因为一个键可能对应多个值
Set<Entry<String,String[]>> entrySet=parameterMap.entrySet();
for(Entry<String,String[]> entry:entrySet) {
String key=entry.getKey();
String[] value=entry.getValue();
for(String v: value) {
System.out.println(key+"---"+value);
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}