Plan du site  
pixel
pixel

Articles - Étudiants SUPINFO

My first JEE web application

Par Oweun VASSARD Publié le 23/03/2017 à 13:33:15 Noter cet article:
(0 votes)
Avis favorable du comité de lecture

Installation

To start, we will install our IDE and our servlet container. To do this, go to the eclipse link and select your operating system: http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplerr launch your download and unpack the In a folder. We are going to create our Tomcat server which allows to contain our Servlet. To do this, launch your eclipse in administrator mode. Make "File → new → Other" type in the "server" search and select the following and then select the "Apache" folder selected your version then done next, enter the backup directory of your server and then click "Download and install »→ Accept the terms and click on« Finish ». A new project is in your explorer, on the bottom tab choose "server" and launch your new server if it shows "[Started, Synchronized]" that your server is operational. You can go to localhost:8080 and see your Tomcat.

Creation of the project

We will now proceed to the creation of our project.

Creation

To do this, click on "File → New → Dynamic Web Project" enter the name of your application then select the server create previously and finally click "Next → Next" check the box "Generate web.xml deployment descriptor" and end by clicking on "Finish"

Description

Now that the project is created we have files that are creating all the code "Backend" will be in the "Java Resources" folder, to use JavaScript in the application there is the folder "Javascipt Resources" and the folder "WebContent" allows you to store html pages.

Servlet

Principle

From our client (browser) we will ask for a URL, it corresponds to requests "HTTP" our servlet engines (Tomcat) will transfer them to the servlets the requests and then the servlets (java class) return their response to the servlet engine Then returns the response to the client.

Creating a servlet

In order to create your first servlet we must create a "Package" which is a group to structure your project.

All our servlets will be found in this package in order to respect the conventions. To better understand the principle of servlets we will create a java class and enter all the information ourselves. On the package we make right click "New → Class" to name it there, without forgetting the conventions (class name then "Servlet") and "Finish".

Now add "extends HttpServlet" to extend the class to add the reference. At each call of a servlet a "doGet" method is executed that is embedded in HttpServlet and we will redefine it to modify our information for displaying our site. To do this, we say "doGet", press "Ctrl + Space" and select the service with "HttpServletResponse", in the same way we will define the method "doPost" which retrieves the information passed in a form. In the GET method we will call a JSP page that will contain our HTML code. Our servlet structure is ready, we will now see the principle of the filters.

The filters

The filter is called between the client (browser) and the servlet, it allows to execute a portion of code before arriving at the servlet then after passing in the JSP. You can have multiple filters in cascade. The filters must be contained in a package (with the same naming conventions as for our Servlet package) we add a class java then we implement the class "Filter", we add the import "javax. Servlet "and then add the three methods to implement.

We add a tag in your "web.xml" file so that our filter is taken into account for each request. Here is the code we have for our filter to work.

Creating our JSP page

doGet

Our JSP page is our view that will be called by our servlet, how to link them? In the "WEB-INF" folder we create an "index.jsp" file and a "pages" folder that will contain a JSP page "login.jsp". We enter our "web.xml" to add a path to our page.

In order to do the mapping of our servlet we will declare it in our "web.xml".

For a question of quality I just implemented Bootstrap which allows to stylize your site fast enough thanks to the different classes already configure. Now that our form is created you have to retrieve the information to see if it exists.

doPost

Now we are in the part of "doPost", the main goal is to retrieve information sent in the form (check though you put the post method in your HTML structure) to retrieve the information just make a "getParameter ( "Name_of_parameter") "on the response.

The sessions

Now that we know how to retrieve information from our connection page, we will exploit the information to know if the filled fields correspond to the authorized user. In our post method of our login we make a condition to know if our user has access. After making the condition we will create a new package "model" then add a class "User". I added my properties (with its accessors). We add a public constructor in our model.

In the servlet I add the fact that if our condition is good we add a new variable in session then we redirect to a protected page (this page does not exist yet we will redirect on itself in the meantime). We will create our new page which is protected by our identification form. In the same way that we did for our first servlet we will create our JSP and declare it in our "web.xml", and bah no we will see another way to declare our JSP. Here is our JSP and the overlay above the class allows to make the extension in the url without having to manage the mapping in our "web.xml".

To secure our page we will now integrate Java code into our JSP page so we will check that the person has gone through the form to reach our secure page.

Passing a variable in our JSP

We create our JSP.

What we can see in the image is that between our tags «

» We have a lot of Java code while outside of these tags is Html code. We add a new condition that checks if we have a value in our session variable.

Now that our page is secure we want to display data on our page for that we want the data to be dynamic. To do this, we pass a new variable in the query with the "setAttribute"

Finally in the JSP page we will display the values in a parameter in session.

Result

Our "Login" page

Our "Secure" page

Source code

Web.xml

Filtre Counter

Model User

Servlet Login

Servlet Secure

Jsp Login

Jsp Secure

You have created your first JEE web application, I hope you enjoy this course and thank you to have read it to the end.

A propos de SUPINFO | Contacts & adresses | Enseigner à SUPINFO | Presse | Conditions d'utilisation & Copyright | Respect de la vie privée | Investir
Logo de la société Cisco, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société IBM, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sun-Oracle, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Apple, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sybase, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Novell, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Intel, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Accenture, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société SAP, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Prometric, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Toeic, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo du IT Academy Program par Microsoft, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management

SUPINFO International University
Ecole d'Informatique - IT School
École Supérieure d'Informatique de Paris, leader en France
La Grande Ecole de l'informatique, du numérique et du management
Fondée en 1965, reconnue par l'État. Titre Bac+5 certifié au niveau I.
SUPINFO International University is globally operated by EDUCINVEST Belgium - Avenue Louise, 534 - 1050 Brussels