A, Tomcat integration
Two, servletLife servlet life cycle
1, initialization
2, run ------ calls
3, destruction (destruction from memory)
three,
1, a method for processing requests method: Method doget difference service dopost
1, a new servlet
2, the new jsp: method
Write a form in body form: User login form
Action:提给servlet
method:
Get |
Only treatment method to get method |
Post |
Only treatment method to get method |
3, service that can get method can also handle post processing method
4, if there are service get while in the servlet post
Priority service
5、
in case
Super.service (arg0, arg1): not deleted
Will call the parent class service, you can then call doGet () or doPost ()
service method of the parent class of thunder so
If(doGet()){
doGet();
}else if(doPost()){
doPost();
}
Normal execution preconditions writing the doGet () and doPost () method
Note:
Although the long servlet is loaded into memory, the hard thing is not deleted, but still on a message
405 : request method server does not support
Four, servlet common errors
404:
Resource not found
servlet alias error
Project Name Misspelled
405:
Request method not supported
500:
Web.xml view class mappings error
Class has logic error
Five, Request
Request to store the data object
Archive current request information request
Note: request object created by the tomcat, arguments passed to the service
1, how to obtain the requested data
Acquisition request header
Way acquisition request
getMethod()
获取请求URL
getRequestURL()
getRequestURI()
获取协议
getScheme()
获取请求行信息
getHeader("键值名");----------事先知道键名
getHeadersname("");------获得所有建植信息
Enumeration
While(e.hasnext){
Syso(e.nextelement);
}
获取用户数据
request并不区分get和post方法,因此此时已经解析完成了
Req.getParameter("键值名称");
如果出现 返回的是null 后台写的键值名不存在或者不一致
乱码出现的原因取数据和存数据的编码不一致
getParameter()不能获取同键不同值的数据(checkbox)
getParameterValues()可以
注意空指针异常
获取键值名
getParameterNames()
六、Response
1、提取数据
2、处理请求
数据库操作
3、处理结果
设置响应头
setHeader("键","值") |
set覆盖上一个同键 |
目前不可为中文 |
addHeader("键","值") |
add与上一个一起出现再响应头中 |
目前不可为中文 |
sendError(错误代码,"语句") |
出现错误的时候出现 |
|
设置
"conten-type" "text/html;charset=utf-8"
setContentType("text/html;charset=utf-8")
设置响应实体
getWriter.write("String:msg")