0

Sorry, I think this is simple question, but I can't figure it out. (Im new in Java programming, and trying to make web servlet). I don't know what else too add here. Servlet code was from internet tutorial. But server won't show it.

My Servlet:

package CarWorld;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class Services_Create extends HttpServlet {

  // Method to handle GET method request.
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");

      PrintWriter out = response.getWriter();
      String title = "Using GET Method to Read Form Data";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +
      "transitional//en\">\n";
      out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>First Name</b>: "
                + request.getParameter("first_name") + "\n" +
                "  <li><b>Last Name</b>: "
                + request.getParameter("last_name") + "\n" +
                "</ul>\n" +
                "</body></html>");
  }
  // Method to handle POST method request.
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }
}

And my web.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <servlet>
        <servlet-name>Services_Create</servlet-name>
        <servlet-class>Services_Create</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>Services_Create</servlet-name>
        <url-pattern>/Services_Create</url-pattern>
    </servlet-mapping>
</web-app>
trakasi
  • 27
  • 8
  • url-pattern should be something like *.html. You don't type Servicies_Create into your browser, do you? – Gyro Gearloose Apr 11 '16 at 14:21
  • as @GyroGearloose said in the tag you have to write something like *.html or *.php anything you want but it should respect a pattern form and in the url of your browzer you type just http://localhost/nameOfYourProject/test.php for example and it will work . another thing easier try to work with "@annotation" and avoid the xml file – PacMan Apr 11 '16 at 14:30
  • @GyroGearloose Still same problem.. https://imgur.com/UaHjSS5 – trakasi Apr 11 '16 at 14:47
  • have you tried not using the underscore for the class name? `Services_Create`. Have you tried changing it to ServicesCreate? – João Rebelo Apr 11 '16 at 16:52
  • Also, have you tried defining the entry of your servlet has ` Services_Create CarWorld.Services_Create ` and access it as _localhost:8080/CarWorldWEB/Services_Create_? – João Rebelo Apr 11 '16 at 16:56
  • @JoãoRebelo Yes, I tried it. ;) – trakasi Apr 11 '16 at 16:59
  • when you deploy the application does it show anything on the log file of tomcat (assuming you're using tomcat as the container)? – João Rebelo Apr 11 '16 at 17:06

1 Answers1

0

Well, I think your problem is in your web.xml file, you have something like this:

<url-pattern>/Services_Create</url-pattern>

But it isn't a pattern, Services_Create is your class, in a pattern you must to write the available pages for your site, like *.html, *.php, *.xhtml, your mistake it write

/Services_Create

When you use "/" you are talking about a root and inside it, many pages, for example

/pages/welcome.xhtml

Inside folder "pages" you have a welcome.xhtml file to display a content. So you must to change your value from /Services_Create, to your personal pattern like *.html or whatever you are using in your project.

Hope it helps to you.

  • I can't understand. I copy code from this tutorial. I nothing change and it dosn't work : http://www.tutorialspoint.com/servlets/servlets-database-access.html (Cannot resolve Variables) – trakasi Apr 11 '16 at 16:48