- 1、本文档共7页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
JavaWeb参考资料JavaWeb参考资料
JavaWeb参考资料(以书上为主)
Servlet产生文档如word、excel、图形等。P 48-49
public class ExcelServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
//指定页面在传输过程中使用的编码方式
response.setHeader(Content-Encoding,gb2312);
response.setContentType(application/vnd.ms-excel;charset=gb2312);
PrintWriter out = response.getWriter();
out.println(学号\t姓名\t性别\t年龄\t所在系);
out.println(95001\t李勇\t男\t20\t信息);
out.println(95002\t刘晨\t女\t19\t数学);
}}
下载文档。P50
public class FileDownloadServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
response.setContentType(application/jar);
// 设置响应头,将响应内容以指定的文件名存储到客户机上
response.setHeader(Content-Disposition,attachment;filename=servlet-api.jar);
File f = new File(C:\\servlet-api.jar);
FileInputStream is = new FileInputStream(f);
ServletOutputStream os = response.getOutputStream();
byte[] bytearray = new byte[1024];
int bytesread = 0;
while( (bytesread = is.read(bytearray) ) != -1 ){
os.write(bytearray, 0, bytesread); }
os.flush();
} }
3、Cookie的设置与读取。P 95-96
向客户Cookie对象:
public class SendCookieServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
Cookie userCookie = new Cookie(username, hacker);
userCookie. setMaxAge(60*60*24*7);
response.addCookie(userCookie);
response.setContentType(text/html);
PrintWriter out=response.getWriter();
out.println(htmltitleadd cookies/title);
out.println(bodyh2A cookie has been sent to browser/h2/body);
out.println(/html);
}}
从客户端读取Cookie:
public class GetCookieServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
String cooki
文档评论(0)