Setup and run helloworld project
Setup system
Java 8
- Link download: CLICK
- Config Maven java: https://www.mkyong.com/maven/how-to-install-maven-in-windows/
MYSQL 8.0.13
- Link download: CLICK
Tomcat 8
- Link download: CLICK
Apache maven 3.6
- Link download: CLICK
- Config MAVEN: https://www.mkyong.com/maven/how-to-install-maven-in-windows/
Eclipse jee 2018
- Link download: https://drive.google.com/file/d/1K0xN6kWR1okHZpcWh8hplotauNrNBqPq/view?usp=sharing
Create a simple Maven project
Navigate to File | New | Project; a New Project wizard window will appear.
Select Maven Project from the list and click on the Next button.
Now, a New Maven Project dialog window will appear; just select the checkbox that has the Create a simple project (skip archetype selection) caption, and click on the Next button
The wizard will ask you to specify artifact-related information for your project; just enter Group Id as com.webjdbc (this is the domain of the website), Artifact Id as webjdbc (this the name of website). Then, select Packaging as war and click on the Finish button
Edit Pom file
Maven will take care of managing dependencies and packaging the project.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.webjdbc</groupId>
<artifactId>webjdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- We declare the versions of dependencies that will be used in our project. This will be defined one time here then called as a variable later when we use -->
<properties>
<serverlet.api.version>3.1.0</serverlet.api.version>
<jsp.api.version>2.0</jsp.api.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jstl.version>1.2</jstl.version>
</properties>
<dependencies>
<!-- Go to this link to copy the code https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.api.version}</version>
</dependency>
<!-- Go to this link to copy the code https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${serverlet.api.version}</version>
</dependency>
<!-- Using jstl for annotation https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Create the index.jsp in the folder webapp/WEB-INF
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:redirect url="/home"/>
Create views: create views folder in webapp
create admin view: create admin folder in views. Then create home.jsp
create web view: create web folder in views, then create home.jsp
Create package com.webjdbc.controller.web and com.webjdbc.controller.admin
Create class HomeController in web
package com.web1.controller.web;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//This is the annotation use to navigate the url based on the pattern
//we want to load the home page when we get the pattern /home
@WebServlet(urlPatterns = {"/home"})
public class HomeController extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 7576609465948154170L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//redirect to the home.jsp
RequestDispatcher rd = request.getRequestDispatcher("/views/web/home.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Similarly, we create HomeController class in admin package
package com.web1.controller.admin;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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(urlPatterns="/admin-home")
public class HomeController extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -8109469024527924551L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Add web server. Browse to the tomcat installation directory. Then click Run on Server