为什么我一请求servlet就弹出下载页面

光凭兴趣别人不会理解 2013-09-17 10:36:39
最近在学servlet,按书本上的demo写,出现请求servlet网页就出现下载框的问题,求解
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ch5_example</display-name>

<servlet>
<servlet-name>ListenerTester</servlet-name>
<servlet-class>com.example.web.ListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenTest.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.example.web.MyServletContextListener</listener-class>
</listener>

<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>
</web-app>

Dog类:
public class Dog {

private String breed;

public Dog(String breed) {
super();
this.breed = breed;
}

public String getBreed() {
return breed;
}

public void setBreed(String breed) {
this.breed = breed;
}

}

ListenerTester:
public class ListenerTester extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ListenerTester() {
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
response.setContentType("test/html");
PrintWriter out = response.getWriter();

out.println("test context attributes set by listener<br>");

out.println("<br>");

Dog dog = (Dog) getServletContext().getAttribute("dog");

out.println("Dog' breed is: " + dog.getBreed());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

监听:

public class MyServletContextListener implements ServletContextListener {

/**
* Default constructor.
*/
public MyServletContextListener() {
// TODO Auto-generated constructor stub
}

/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
sc.setAttribute("dog", d);

}

/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}

}

敲地址http://localhost:8080/ListenTest.do 就出现下载,求解
...全文
113 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
引用 1 楼 ghostkngiht 的回复:
response.setContentType("test/html"); 不是test是text 替换成response.setContentType("text/html;charset=UTF-8");试试
感谢!我太不小心了!
  • 打赏
  • 举报
回复 1
末日哥 2013-09-17
1L说得对。。
  • 打赏
  • 举报
回复
ghostkngiht 2013-09-17
response.setContentType("test/html"); 不是test是text 替换成response.setContentType("text/html;charset=UTF-8");试试
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
社区管理员
  • Web 开发社区
申请成为版主
帖子事件
创建了帖子
2013-09-17 10:36
社区公告
暂无公告