0

I'd like to code my own JavaEE-App. I'm using Linux Mint and Glassfish 4 as an App-Server. My Eclipse is Oxygen (EE-Version).

I'd like to test if my Costumer is able to sign in.

I haven't include CSS yet so it might look a bit boring. :D

These are my projects in Eclipse:

enter image description here

This is my Index-HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
   <link rel="stylesheet" href="css/styles.css">
   <title>Onlineshop</title>
</head>
<body>
    <header>
        <hgroup>
        <h1 class="title">Onlineshop</h1>
        </hgroup>
        <nav>
          <a href="index.html">Home</a>
          <a href="register.html">Registrieren</a>
          <a href="signin.html">Einloggen</a>
          <a href="sell.html">Verkaufen</a>
          <a href="search.html">Suchen</a>
        </nav>
</header>

<footer>
        Copyright
</footer>
</body>

"Registrieren" means "sign up", "Einloggen" means "to log in", "Verkaufen" means "to sell" and "Suchen" means "to search".

This is the HTML-Site which is displayed after clicking on "Registrieren":

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
<link rel="stylesheet" href="css/styles.css">
   <title>Onlineshop</title>
</head>
<body>
    <header>
        <hgroup>
        <h1 class="title">Onlineshop</h1>
        </hgroup>
        <nav>
          <a href="index.html">Home</a>
          <a href="register.html">Registrieren</a>
          <a href="signin.html">Einloggen</a>
          <a href="sell.html">Verkaufen</a>
          <a href="search.html">Suchen</a>
        </nav>
</header>
<form action="register" method="post">
<fieldset>
<legend>Registrieren</legend>
<table>
    <tbody>
    <tr>
        <th>
            <label for="email">Email:</label>
        </th>
        <td>
            <input 
                type="email" 
                name="email"
                size="40"
                maxlength="40"
                title="muster@beispiel.de"
                placeholder="E-Mail eingeben"
                pattern=".{6,40}" 
                required="required">
        </td>
    </tr>
    <tr>
        <th>
            <label for="password">
                Password:
            </label>
        </th>
        <td>
            <input 
                type="password" 
                name="password"
                size="10"
                maxlength="10"
                title="6-10 Zeichen"
                placeholder=
                    "Passwort eingeben"
                pattern=".{6,10}" 
                required="required">
        </td>
    </tr>
    <tr>
        <td/><td>
            <input type="submit">       
            <input type="reset">        
        </td>
    </tr>
</tbody>
</table>
</fieldset>
</form>

<footer>
        Copyright 
</footer>
</body>

This is my Servlet:

package de.java2enterprise.onlineshop;

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

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


@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public RegisterServlet() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse    
  response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse 
 response) 
        throws ServletException, IOException {

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

    final String email = request.getParameter("email");
    final String password = request.getParameter("password");

    final PrintWriter out = response.getWriter();

    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<body>");
    out.println("<br> Ihre Eingaben");
    out.println("<br> EMail: " + email);
    out.println("<br> EMail: " + password);
    out.println("</body>");
    out.println("</html>");

 }

}

I didn't write an Web.xml because i just want to cope it with annotations. Now I have 2 questions:

All the imports of the Servlet-API can't be resolved. But why? I did create a Java-Enterprise-Application-Project and assigned the Webproject onlineshop-war to the App. And the Glassfish is assigned to the Java-Enterprise-Application-Project and on the Glassfish-Server is the Servlet-API recorded, isn't it?

And the Second question: Why can't he find the Servlet after clicking on submit after typing in the EMail and the Password on the register-HTML? Has it something to do with question 1?

Thank you very much!

Lars Laf
  • 57
  • 4

1 Answers1

1

First Question -

All the imports of the Servlet-API can't be resolved. But why? I did create a Java-Enterprise-Application-Project and assigned the Webproject onlineshop-war to the App. And the Glassfish is assigned to the Java-Enterprise-Application-Project and on the Glassfish-Server is the Servlet-API recorded, isn't it?

I am using eclipse with combination of Java-8 and Tomcat 8.5 web container, by watching your question, I simply created a new dynamic web project in eclipse, later created two html files index.html and register.html along with Servlet class RegisterServlet in the same package you defined.

After doing all work, I simply first started and stopped my web container server, later make a Right click on the project name, Go to menu Run As > Run on Server > Choose an existing server, there I choose the Tomcat 8.5 Server at localhost > Remove all other projects from list Configured instead the current one and pressed the Finish.

Earlier I have selected I have selected the default browser by steps option Window(Menu)>Web Browser>Chrome.

No such web.xml configuration file is there in directory WebContent/WEB-INF/ inside my project.

Thus the Tomcat 8.5 server will start and show the index.html page in chrome new tab, there Optionshop page appeared and I chose the Registrieren link, then redirected to register.html, there I inserted email id and password later pressed Enter and the servlet mapping viewed me my email id and password in fresh page, that's it.

Second Question -

Why can't he find the Servlet after clicking on submit after typing in the EMail and the Password on the register-HTML? Has it something to do with question 1?

I changed the @WebServlet to /RegisterServlet and in register.html I replaced the form starting tag to

<form name="register" method="POST" action="RegisterServlet">

also in Servlet class the method doGet(HttpServletRequest request, HttpServletResponse response) was missing, so I create one and my Servlet class is as follows:

package de.java2enterprise.onlineshop;

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

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

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public RegisterServlet() { }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");

        final String email = request.getParameter("email");
        final String password = request.getParameter("password");

        final PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<body>");
        out.println("<br> Ihre Eingaben");
        out.println("<br> EMail: " + email);
        out.println("<br> EMail: " + password);
        out.println("</body>");
        out.println("</html>");

        out.close();
    }
}

Hope this will help you, thanks.

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48
  • Thank you very much. I have stuck to my solution indeed but by creating the projects and the server from the ground up again it did work. So thank you for this hint. – Lars Laf Jan 07 '18 at 19:26