servletResponse 封装了响应信息。
l getWriter()
返回PrintWriter对象,调用其print方法可以把print中的参数直接打印到客户的浏览器上。如:
在LoginServlet中的service中添加如下代码:
PrintWriter out = response.getWriter();
out.print("helloword!");
访问网页,输出文字:
l setContextType(String sType)
设置响应的内容类型。
返回一个html?js?或一个word文档。等等。
如返回一个word文档:参数写什么呢,打开[D:\apache-tomcat-8.0.17\conf]下的web.xml文件,搜索[doc]或[word],找到如下位置:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
在上面的代码前添加如下代码:
response.setContentType("pplication/msword");
PrintWriter out = response.getWriter();
out.print("helloword!");
在页面中输入http://localhost:8080/MyWebProject/urlLogin
弹出了一个下载对话框,
给名称加个后辍.doc,生成本地文件:
打开文件内容为helloword!