Reputation: 417
I have a lot of experience in servlets and tomcat, but today was the second time I encountered something strange: after playing around with the servlet doGet logic, when I accessed the servlet it started download the file (instead of execute the logic!). It happens in all browsers, and it happens also when I deploy the webapp on different configuration of the tomcat (but still, on eclipse. maybe the bug is in the tomcat eclipse plugin?)
The logic I wrote is not something that might be related to the content type. When I tried to change the content type - it still consider the servlet as a file, and tried to download it.
Does someone know this bug?
EDIT: Here are the examples-
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.StoreItems;
/**
* Servlet implementation class Controller
*/
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
String supportMail ;
int hitCounter =0;
String counter ;
StoreItems store;
public Controller() {
super();
}
@Override
public void init() throws ServletException {
supportMail = getInitParameter("email");
store = new StoreItems();
counter = getInitParameter("hitCounter");
hitCounter = Integer.parseInt(counter);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
response.getWriter().write(hitCounter);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
The web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>OnlineStore</display-name>
<servlet>
<description>
</description>
<display-name>Controller</display-name>
<servlet-name>Controller</servlet-name>
<servlet-class>Controller</servlet-class>
<init-param>
<description>
</description>
<param-name>email</param-name>
<param-value>[email protected]</param-value>
</init-param>
<init-param>
<param-name>hitCounter</param-name>
<param-value>7</param-value>
</init-param>
</servlet>
<servlet>
<description>
</description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Varsions: Tomcat 7, eclipse indigo, java 6, servlet api 2.4. I tried to change eclipse workspace, and it still did not work.
Upvotes: 2
Views: 6002
Reputation: 31
Set response as text.html
response.setContentType("text/html");
I am sure it will resolve the problem.
Upvotes: 3
Reputation: 61
Btw if you return online text and that's exactly what your servlet does with the content type text/plain, you should rather use for example jquery ajax to parse the text result and render it into the html or jsp from which the request is issued.
If you use the content type text/html you should see the result but then at least wrap it into the minimum html tags your value in your servlet.
Regards s
Upvotes: 0
Reputation: 61
response.getWriter().write(hitCounter).flush();
should show you a result
Upvotes: 0
Reputation: 17846
What is downloaded? The servlet source code? You have to compile the code to a .class file and put it in WEB-INF/classes. Eclipse should do that automatically if you put your java file in a Java Source folder.
Upvotes: 0