Justin
Justin

Reputation: 1249

Tomcat allows me only to download servlets

I have a problem with Tomcat in Eclipse; when I run a Simple Servlet , Tomcat(in Eclipse) doesn't show me the page but it asks me to download it! There is the code. What can I do to fix it? The source code is in ..Documents\Workspace\Servlet02\src\mypkg\SimpleServlet

package mypkg;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.*;

@SuppressWarnings("unused")
@WebServlet(name = "SimpleServlet", urlPatterns = {"/SimpleServlet"})
public class SimpleServlet extends HttpServlet {

    Date currDateAndTime;

    private static final long serialVersionUID = 1L;

    protected void processRequest(HttpServletRequest request , HttpServletResponse response) throws IOException , ServletException{
        response.setContentType("html/text ; charset=UTF-8 ");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CurrentDateAndTime</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CurrentDateAndTime at " + request.getContextPath() + "</h1>");
            out.println("<br/>");
            synchronized(currDateAndTime){
                currDateAndTime = new Date();
                out.println("The current date and time is: " + currDateAndTime);
            }
            out.println("</body>");
            out.println("</html>");
            } finally {
            out.close();
            }
    }
    @Override
    protected void doGet(HttpServletRequest request , HttpServletResponse response)  throws IOException , ServletException{
        processRequest(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request , HttpServletResponse response) throws IOException , ServletException{
        processRequest(request,response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


}

Upvotes: 0

Views: 93

Answers (2)

Niru
Niru

Reputation: 742

It should be

 response.setContentType("text/html");
 response.setCharacterEncoding("UTF-8");

Upvotes: 0

Braj
Braj

Reputation: 46871

The problem is due to below line.

response.setContentType("html/text ; charset=UTF-8 ");

It should be

response.setContentType("text/html; charset=UTF-8");

Or remove it completely or use

response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");

Read more about it in Documentation

Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.

Upvotes: 3

Related Questions