一、从容器到HttpServlet
1、web容器作了什么
web容器做的事情就是,创建Servlet实例,并完成Servlet的名称注册及URL模式的对应。在请求来到时,web容器会转发给正确的Servlet来处理请求。
当请求来到http服务器时,而http服务器转交请求给容器时,容器会创建一个代表档次请求的HttpServletRequest对象,并将请求相关信息设置给该对象。同时,容器会创建一个HttpServletResponse对象,作为稍后要对客户端进行相应的java对象。
接着,容器会根据读取的@webServlet标注或者xml的设置,找出处理该请求的servlet,调用它的service()方法,将创建的HttpServletRequest对象、HttpServletResponse对象传入作为参数,service()方法中会根据HTTP请求的方式,调用对应的doXXX()方法。接着再doXXX()方法中,可以使用request、resposne对象,如:getParameter()取得请求参数,使用getWriter()取得输出用的PrintWriter对象,并进行各项响应处理。对PrintWriter做的输出操作,最后由容器转换为HTTP响应,再由HTTP服务器对浏览器响应。之后容器将对象销毁。
2、关于HttpServletRequest
2.1 处理请求参数与标头
String username = request.getParameter("name");指定请求参数名称来取得对应的值。如果传来的是"123"这样的字符串值,必须使用Integer.parseInt()这类方法剖析为基本类型。
String[] values = request.getParameterValues("param");就像param=10¶m=20¶m=30,此时用getParameterValues()取得一个String数组。
如果想要知道请求中有多少请求参数,则可以使用getParameterNames()方法,会返回一个Enumeration对象。
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()){
String param = e.nextElement();
...
}
对于HTTP标头的信息(Header),可以通过以下方法取得:
1、getHeader():使用方式与getParameter类似。
2、getHeaders():与getParameterValues类似。
3、getHeaderNames():与getParameterNames类似。
以下的实例如何取得并显示浏览器送出的标头信息。
package cc.openhome; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class HeaderServlet
*/
@WebServlet("/header.view")
public class HeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public HeaderServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>HeadServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>HeaderServlet at "+request.getContextPath()+"</h1>");//取得应用程序环境路径
Enumeration<String> names=request.getHeaderNames();//取得所有标头名称
while(names.hasMoreElements()){
String name=names.nextElement();
out.println(name+": "+request.getHeader(name)+"<br>");//输出所有的标头值
}
out.println("</body>");
out.println("</html>");
out.close();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
HeaderServlet
运行结果:
2.2 getReader()、getInputStream()读取Body内容
getReader()方法,可以取得一个BufferedReader对象,可以读取Body数据。
例子1:读取请求Body内容
package cc.openhome; import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class BodyServlet
*/
@WebServlet("/body.view")
public class BodyServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public BodyServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub } private String readBody(HttpServletRequest request) throws IOException {
// TODO Auto-generated method stub
BufferedReader reader=request.getReader();//取得BufferedReader对象
String input=null;
String requestBody="";
while((input=reader.readLine())!=null){
requestBody=requestBody+input+"<br>";
}
return requestBody;
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String body=readBody(request);
PrintWriter out=response.getWriter();//取得响应输出对象
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BodyView</title>");
out.println("</head>");
out.println("<body>");
out.println(body);
out.println("</body>");
out.println("</html>");
out.close();
} }
BodyServlet
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="body.view" method="post">
名称:<input type="text" name="user"><br>
密码:<input type="password" name="passwd"><br><br>
<input type="submit" name="login" value="送出">
</form>
</body>
</html>
form.html
在名称字段上输入“良葛格”,密码上输入“123456”,单机送出按钮,得到:
2.3 getPart()、getParts()取得上传文件
Servlet3.0中,新增了Part接口,可以方便进行文件上传处理。可以通过request对象的getPart()方法取得Part对象。
例1:上传文件到指定目录。
package cc.openhome; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part; /**
* Servlet implementation class PhotoServlet
*/
@MultipartConfig(location="e:/java web/workspace/")//设置location属性
@WebServlet("/photo.do")
public class PhotoServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public PhotoServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
Part part=request.getPart("photo");
String filename=getFilename(part);
//writeTo(filename,part);
part.write(filename);//将文件写入location的指定目录
} // private void writeTo(String filename, Part part) throws IOException {
// // TODO Auto-generated method stub
// InputStream in=part.getInputStream();
// OutputStream out=new FileOutputStream("e:/java web/workspace/"+filename);
// byte[] buffer=new byte[1024];
// int length=-1;
// while((length=in.read(buffer))!=-1){
// out.write(buffer,0,length);
// }
// in.close();
// out.close();
// } private String getFilename(Part part) {
// TODO Auto-generated method stub
String header=part.getHeader("Content-Disposition");
String filename=header.substring(header.indexOf("filename=\"")+10,header.lastIndexOf("\""));
return filename; } }
UploadServlet.java
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Insert title here</title>
</head>
<body>
<form action="photo.do" method="post" enctype="multipart/form-data">
上传相片:<input type="file" name="photo" value="" /><br>
<input type="submit" name="upload" value="上传">
</form>
</body>
</html>
upload.html
在Tomcat中必须设置@MultipartConfig标注才能使用getPart()相关API。@MultipartConfig也可以设置属性,比如location。@MultipartConfig(location="c:/workspace"),则上例子就可以修改为:
package cc.openhome; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part; /**
* Servlet implementation class PhotoServlet
*/
@MultipartConfig(location="e:/java web/workspace/")//设置location属性
@WebServlet("/photo.do")
public class PhotoServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public PhotoServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
Part part=request.getPart("photo");
String filename=getFilename(part);
//writeTo(filename,part);
part.write(filename);//将文件写入location的指定目录
} // private void writeTo(String filename, Part part) throws IOException {
// // TODO Auto-generated method stub
// InputStream in=part.getInputStream();
// OutputStream out=new FileOutputStream("e:/java web/workspace/"+filename);
// byte[] buffer=new byte[1024];
// int length=-1;
// while((length=in.read(buffer))!=-1){
// out.write(buffer,0,length);
// }
// in.close();
// out.close();
// } private String getFilename(Part part) {
// TODO Auto-generated method stub
String header=part.getHeader("Content-Disposition");
String filename=header.substring(header.indexOf("filename=\"")+10,header.lastIndexOf("\""));
return filename; } }
UploadServlet.java
如果有多个文件要上传,可以使用getParts方法,这回返回一个Collection<Part>,其中是每个上传文件的part对象。
例子2:上传多个文件
package cc.openhome; import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class UploadServlet
*/
@WebServlet("/upload.do")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
//锟斤拷取锟斤拷锟斤拷Body
byte[] body=readBody(request);
//取锟斤拷锟斤拷锟斤拷Body锟斤拷锟捷碉拷锟街凤拷锟斤拷锟斤拷示
String textBody=new String(body,"UTF-8");
//取锟斤拷锟较达拷锟斤拷锟斤拷锟斤拷锟侥硷拷
String filename=getFilename(textBody);
//取锟斤拷锟侥硷拷锟斤拷始锟斤拷锟斤拷锟轿伙拷锟�
Position p=getFilePosition(request,textBody);
//锟斤拷锟斤拷锟斤拷募锟�
writeTo(filename,body,p);
} private void writeTo(String filename, byte[] body, Position p) throws IOException {
// TODO Auto-generated method stub
FileOutputStream fileOutputStream=new FileOutputStream("e:/java web/workspace/"+filename);
fileOutputStream.write(body,p.begin,(p.end-p.begin));
fileOutputStream.flush();
fileOutputStream.close();
} private Position getFilePosition(HttpServletRequest request, String textBody) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
//取得实际上传的文件
String contentType=request.getContentType();
String boundaryText=contentType.substring(contentType.lastIndexOf("=")+1,contentType.length());
//取得实际上传的文件
int pos=textBody.indexOf("filename=\"");
pos=textBody.indexOf("\n",pos)+1;
pos=textBody.indexOf("\n",pos)+1;
pos=textBody.indexOf("\n",pos)+1;
int boundaryLoc=textBody.indexOf(boundaryText,pos)-4;
int begin=((textBody.substring(0,pos)).getBytes("GBK")).length;
int end=((textBody.substring(0,boundaryLoc)).getBytes("GBK")).length;
return new Position(begin, end);
} private String getFilename(String textBody) {
// TODO Auto-generated method stub
String filename=textBody.substring(textBody.indexOf("filename=\"")+10);
filename=filename.substring(0,filename.indexOf("\n"));
filename=filename.substring(filename.lastIndexOf("\\")+1,filename.indexOf("\""));
return filename;
} private byte[] readBody(HttpServletRequest request) throws IOException {
// TODO Auto-generated method stub
int formatDataLength=request.getContentLength();
DataInputStream dataStream=new DataInputStream(request.getInputStream());//取锟斤拷InputStraem锟侥讹拷锟斤拷
byte body[]=new byte[formatDataLength];
int totalBytes=0;
while(totalBytes<formatDataLength){
int bytes=dataStream.read(body,totalBytes,formatDataLength);
totalBytes+=bytes;
}
return body;
} }
class Position{
int begin;
int end;
public Position(int begin,int end) {
// TODO Auto-generated constructor stub
this.begin=begin;
this.end=end;
}
}
UploadServlet2.java
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Insert title here</title>
</head>
<body>
<form action="upload1.do" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="file1" value="" /><br>
文件2:<input type="file" name="file2" value="" /><br>
文件3:<input type="file" name="file3" value="" /><br><br>
<input type="submit" name="upload" value="上传"/>
</form>
</body>
</html>
multi-upload.html
2.4 使用RequestDispatcher调派请求
在web应用程序中,经常需要多个Servlet来完成请求。例如,将另一个Servlet的请求处理流程包含进来,或者将请求转发forward给别的servlet处理。可以使用:
RequestDispatcher dispatcher = request.getRequestDispatcher("some.do");
(1)、使用include()方法
将另一个servlet的操作流程包括至目前的servlet操作之中。
例1:include方法
package cc.openhome; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Some
*/
@WebServlet("/some.view")
public class Some extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Some() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
out.println("Some do one...");
RequestDispatcher dispatcher=request.getRequestDispatcher("other.view?data=123456");
dispatcher.include(request, response);
out.println("Some do two...");
out.close();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
some.java
package cc.openhome; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class OtherServlet
*/
@WebServlet("/other.view")
public class OtherServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public OtherServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
out.println("Other do one..");
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
othersome.java
运行结果:网页上看到响应顺序是Some do one ... Other do one... Some do two.. .
(2)、使用forward()方法
将处理请求转发给别的Servlet。
例2:forwod方法
package cc.openhome; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.sun.javafx.sg.prism.NGShape.Mode;
import com.sun.media.sound.ModelAbstractChannelMixer; /**
* Servlet implementation class HelloController
*/
@WebServlet("/hello.do")
public class HelloController extends HttpServlet {
private static final long serialVersionUID = 1L;
private HelloModel model=new HelloModel();
/**
* @see HttpServlet#HttpServlet()
*/
public HelloController() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name=request.getParameter("user");//收集请求参数
String message=model.doHello(name);//委托HelloModel对象处理
request.setAttribute("message", message);//将结果信息设置至请求对像成属性
request.getRequestDispatcher("hello1.view").forward(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub } }
HelloController.java
package cc.openhome; import java.util.*; public class HelloModel {
private Map<String, String>messages=new HashMap<String,String>();
public HelloModel(){
messages.put("caterpillar", "Hello");
messages.put("Justin", "Welcome");
messages.put("momor", "Hi");
}
public String doHello(String user){
String message=messages.get(user);
return message+", "+user+"!";
}
}
HelloModel.java
package cc.openhome; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class HelloView
*/
@WebServlet("/hello1.view")
public class HelloView extends HttpServlet {
private static final long serialVersionUID = 1L;
private String htmlTemplate=
"<html>"
+ "<head>"
+ "<meta http-equiv='Content-Type'"
+ "content='text/html; charset=UTF-8'>"
+ "<title>%s</title>"
+ "</head>"
+ "<body>"
+ "<h1>%s</h1>"
+ "</body>"
+"</html>";
/**
* @see HttpServlet#HttpServlet()
*/
public HelloView() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String user=request.getParameter("user");//取得请求参数
String message=(String) request.getAttribute("message");//取得请求属性
String html=String.format(htmlTemplate, user,message);//产生html结果
response.getWriter().print(html);//输出html结果 } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
HelloView.java
运行结果:(Model2架构)