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.
We will now proceed to the creation of our project.
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"
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.
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 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.
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.
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.
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.
Our "Login" page
Our "Secure" page
You have created your first JEE web application, I hope you enjoy this course and thank you to have read it to the end.