0

I've enough knowledge to develop in Java SE/JavaFX (Desktop), but it's my 1st time with Java EE (WEB). I'd like to build a basic app only getting Login and Password in a HTML/JSP, calling a Servlet (in Java) and returning a single message to the HTML/JSP. This time I want to do that the "hard way", without any IDE. So, I've installed Tomcat 7.0 and I have those modules:

Java (this example only receive from HTML/JSP)

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class loginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        out.println("<html>");
        out.println("<body>");
        out.println("Hello " + "  " + userName + "welcome to my blog");
        out.println("Your password is : " + "  " + password + "<br>");
        out.println("</body></html>");
    }
}

HTML/JSP

<!DOCTYPE html>
<html lang ="pt-br">        
   <head>
      <title> loginServlet </title>
      <meta http-equiv = ”Content-Type” content=”text/html; charset=UTF-8”>
      <link rel="stylesheet" type="text/css" 
            href="c:/java/html/css/estilo.css"/>                     
   </head>

   <body>
      <h2> Login Page </h2>

      <p>Please enter your username and password</p>

      <form method="GET" action="loginServlet">
         <p> Username <input type="text" name="userName" size="50"> </p>

     <p> Password <input type="text" name="password" size="20"> </p>

     <p> <input type="submit" value="Submit" name="B1"> </p>
      </form>
   </body>
</html>

WEB.XML

<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app 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"
    version="2.4">

    <display-name>loginServlet</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>loginServlet</servlet-name>
        <servlet-class>java.loginServlet.WebContaint.WEB-
                       INF.classes.loginServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>loginServlet</servlet-name>
        <url-pattern>/loginServlet</url-pattern>
    </servlet-mapping>

</web-app> 

Paths and folders

  • loginServlet.class is in C://java/loginServlet/WebContaint/WEB-INF/classes/

  • loginServlet.jsp is in C://java/loginServlet/WebContaint/

  • web.xml is in C://java/loginServlet/WebContaint/WEB-INF/

  • loginServlet.war is in C:\Program Files\Apache Software Foundation\ Tomcat 7.0\webapps

When I try to call in my Chrome, using a localhost (http://localhost:8080/loginServlet), I receive a 404 Error.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Where's the Javascript question? – Ele Jan 30 '18 at 17:52
  • i think that my problem is, probably, in some place between my HTML and my Servlet Java or, maybe, with Tomcat. I don't know. – Lucio Tepe Jr. Jan 30 '18 at 17:56
  • Check out this sample for a web.xml file with Tomcat to see what you might be missing: http://wiki.metawerx.net/wiki/Web.xml To me it looks like you are missing a section, and I think your package name has too much info in it, remove the "WEB-INF" stuff – bakoyaro Feb 01 '18 at 20:33

1 Answers1

0

Your Servlet class name looks wrong:

<servlet-class>java.loginServlet.WebContaint.WEB-
               INF.classes.loginServlet</servlet-class>

I'm not even sure it is possible to place user code in the java namespace (it might be protected), regardless I wouldn't recommend it. Also having WEB-INF.classes in the package name is not right. Splitting the name over multiple lines is also not right.

In fact, in your code, you don't even have a package name, so you should be able to write:

<servlet-class>loginServlet</servlet-class> 

Read this tutorial on creating servlets, which addresses naming of servlet classes and cross-referencing them from a web.xml file.

Note, you don't even need a web.xml file for a simple example like this, you can just use an @WebServlet annotation.


Aside:

  • Current version of Tomcat is 9.x, I wouldn't recommend using old versions like 7.x.
  • Stick to Java naming conventions (e.g. call your class LoginServlet, not loginServlet).
  • To learn JEE, I recommend using the official JEE tutorial.

A stranger thing happaned: it's seem that only (directly) servlet (loginServlet.class) was called. The HTML (loginServlet.jsp) didn't show the page where Login and Password can be informed. And i saw the msg "Hello userName welcome to my blog" that is generate by Servlet (loginServlet.class). With Login and Password null, of course.

From your earlier question, you say that you are invoking the app like this:

http://localhost:8080/loginServlet

That isn't going to tell the app to use the jsp file you have created, which you have named loginServlet.jsp. The breakdown of the call you made is that you are using the http protocol over port 8080 to access your tomcat server. You provide the path /loginServlet, which is identifying your web application (by default, it gets its name from the war file which you deployed, which you called loginServlet.war). Once in the app it will try to open a welcome file if there is one. But you haven't provided a welcome file or an empty name mapping for the servlet, so, normally, I would just expect a 404 not found error for that url.

The url of your servlet would be

http://localhost:8080/loginServlet/loginServlet  

The first loginServlet identifies your app, the second loginServlet identifies the servlet itself. So, accessing that url will directly access the servlet processing.

But, you don't want your user to directly invoke that. Instead, you want your jsp file to show, and then for it to send data to server http://localhost:8080/loginServlet/loginServlet to trigger the servlet processing.

Your two options to access the jsp are either to provide a welcome file or to provide the fully qualified name in your request string.

A fully qualified name request would be:

http://localhost:8080/loginServlet/loginServlet.jsp

If, instead you want to assign a welcome file, you could add the following section to your web.xml:

<welcome-file-list>
    <welcome-file>loginServlet.jsp</welcome-file>
</welcome-file-list>

Then the following access string should bring up the loginServlet.jsp page:

http://localhost:8080/loginServlet
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • jewelsea, thank you for your answer. Actually i'm using the same name in class and html/jsp just as a test. In a real world i'll use the conventions, as i did in my Desktop App. – Lucio Tepe Jr. Jan 30 '18 at 21:50
  • So, as you sugested i changed my WEB.XML and used only "loginServlet" instead whole path. A stranger thing happaned: it's seem that only (directly) servlet (loginServlet.class) was called. The HTML (loginServlet.jsp) didn't show the page where Login and Password can be informed. And i saw the msg "Hello userName welcome to my blog" that is generate by Servlet (loginServlet.class). With Login and Password null, of course. What i'm doing wrong? Is it possible a Browser call directly a class Java? Thanks. – Lucio Tepe Jr. Jan 30 '18 at 21:50
  • See updated answer to address your additional question about how to invoke the jsp file from a browser. – jewelsea Jan 30 '18 at 22:47
  • jewelsea, you're the guy! I've made the change in my web.xml, including the "welcome-file-list" and is everything ok now. Thanks a lot! – Lucio Tepe Jr. Jan 31 '18 at 19:12
  • Done. Thanks again. – Lucio Tepe Jr. Jan 31 '18 at 22:00