0

I am learning how to use servlet and i copied the example in my book but i keep getting a 404 error. here is the code :

package servlet;
import java.io.IOException;
import` java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SampleServlet2
*/
@WebServlet("/SampleServlet2")
public class SampleServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws             ServletException, IOException {
    // TODO Auto-generated method stub
    String[] luckArray = {"超スッキリ","スッキリ","最悪"};
    int index = (int) (Math.random()*3);
    String luck=luckArray[index];
    Date date= new Date();
    SimpleDateFormat sdf=new SimpleDateFormat("MM月dd日");
    String today=sdf.format(date);
    
    response.getWriter().append("Served at: ").append(request.getContextPath());
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>スッキリ占い</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<p>"+today+"の運勢は「"+luck+"」です</p>");
    out.println("</body>");
    out.println("</html>");
}

} ``

I don't really know what to do as the book doesn't include a 404 error and all the topics about servlet are old.

1 Answers1

0

There is no problem in your code. may be the problem is with your server configuration. I am using Eclipse IDE and tomcat 9 , and your code is working fine.

  • I use Tomcat 9 Java 17 too but it doesn't work I tried to do another exercice linking jsp and servlet : the html page of the jsp file opens normally but when it comes to servlet, i keep getting that 404 error – wdwad dwad Jan 11 '23 at 06:39
  • Ok, are you following similar steps, 1. Create new Dynamic web project. 2. in the src/main/java make a package , let name - main 3. make the new servlet the name you mentained. 4. make sure you import correct packages. 5. Import these import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; – Umesh Maurya Jan 11 '23 at 06:49
  • Click on your servlet , Choose run as run on server. choose yorr server. then it should work fine. I am doing this in Eclipse. Other IDE should also work fine – Umesh Maurya Jan 11 '23 at 06:50
  • I think I followed those steps. I am sorry but I need to go to work, I'll come back as soon as I can to test your solution ! thank you very much for your answer ! – wdwad dwad Jan 11 '23 at 06:51
  • ok, no problem. Take your time. – Umesh Maurya Jan 11 '23 at 06:52