0% found this document useful (0 votes)
27 views

Handout 07. Backend by Java Technologies (Environment Setup)

Uploaded by

Ha Cao Thu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Handout 07. Backend by Java Technologies (Environment Setup)

Uploaded by

Ha Cao Thu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

5/21/2024

Web development:
Backend by Java technologies

Environment
• Java JDK/JRE
• 1.8
• https://www.oracle.com/java/technologies/downloads/#java8-windows
• Tomcat
• Java Application Server
• Support Java servlet & JSP
• Tomcat 8 download: https://tomcat.apache.org/download-80.cgi
• Eclipse
• IDE by Java
• Download for Enterprise Java developer:
https://www.eclipse.org/downloads/packages/release/2024-03/r/eclipse-ide-enterprise-
java-and-web-developers
• MySQL
• Server, Workbench & JDBC (J connector)
• Using MySQL Installer: https://dev.mysql.com/downloads/installer

1
5/21/2024

Env. Config PS C:\Users\hoang> dir env:Path

Name Value
---- -----
Path C:\Program
1. Add JDK/bin to $PATH Files\PowerShell\7;C:\Program Files (x86)\Common
(enable execute java and Files\Oracle\Java\java8path…

javac outside JDJ directory) PS C:\Users\hoang> javac -version


2. Set environment variable
javac 1.8.0_411
PS C:\Users\hoang> dir env:CATALINA_HOME
$CATALINA_HOME to Tomcat
installation directory Name
----
Value
-----
3. Start Tocat server by statup CATALINA_HOME D:\working\Tomcat 8.5

script PS C:\Users\hoang> & 'D:\working\Tomcat 8.5\bin\startup.bat'


Using CATALINA_BASE: "D:\working\Tomcat 8.5"
4. Web browser to check Using CATALINA_HOME: "D:\working\Tomcat 8.5"
http://localhost:8080 Using CATALINA_TMPDIR: "D:\working\Tomcat 8.5\temp"
Using JRE_HOME: "D:\working\jdk-1.8\jre"
Using CLASSPATH: "D:\working\Tomcat
8.5\bin\bootstrap.jar;D:\working\Tomcat 8.5\bin\tomcat-juli.jar"
Using CATALINA_OPTS: ""

Understand Tomcat directories


• bin: contains the binaries and scripts (e.g.,
startup.bat and shutdown.bat for Windows;
startup.sh and shutdown.sh for Unixes and macOS).
• conf: contains the system-wide configuration files,
such as server.xml, web.xml, and context.xml.
• webapps: contains the webapps to be deployed.
You can also place the WAR (Webapp Archive) file
for deployment here.
• lib: contains the Tomcat's system-wide library JAR
files, accessible by all webapps. You could also
place external JAR file (such as MySQL JDBC Driver)
here.
• logs: contains Tomcat's log files. You may need to
check for error messages here.
• work: Tomcat's working directory used by JSP, for
JSP-to-Servlet conversion.

2
5/21/2024

Understand Tomcat config files


• The Tomcat configuration files, in XML format, are located in the "conf" sub-
directory of your Tomcat installed directory, e.g. “D:\working\Tomcat
8.5\conf". The important configuration files are:
• server.xml: protocols, ports, SSL
• web.xml: working directory for
web applications in server
• context.xml: web application
• Make a BACKUP of the configuration files
before you proceed!!!

First Web Application


• Goto Tomcat's "webapps" sub-directory and
create the following directory structure & files
(as illustrated).
• These directory names are case-sensitive!!!
Don't make typo error!!! Double check the
SPELLING again and again!!!
• "hello": the context root (or document base)
of your webapp. You should keep all your HTML files and resources visible to the web users
(e.g., HTMLs, CSSs, images, scripts, JSPs) under this context root.
• "hello/WEB-INF": This directory, although under the context root, is a secure directory (i.e.,
not visible to web users). It is used to store resources that should not be directly accessible
by clients; but accessible through servlets or other server-side components. This is also
where you keep your application's web descriptor file "web.xml".
• "hello/WEB-INF/classes": This is where you keep all the Java classes such as servlet class-
files.

3
5/21/2024

First Web App: HelloHome.html static file


• File HelloHome.html

<!DOCTYPE html>
<html>
<head><title>My Home Page</title></head>
<body>
<h1>Hello, world!</h1>
<p>My Name is so and so. This is my HOME.</p>
</body>
</html>

First Web App: HelloServlet


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
// The doGet() runs once per HTTP GET request to this servlet.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// Set the response's MIME type of the response message
response.setContentType("text/html");
// Allocate an output writer to write the response message into the network socket
PrintWriter out = response.getWriter(); // throw IOException
// Write the response message, in an HTML page
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title>Hello, World</title></head>");
out.println("<body>");
out.println("<h1>Hello, world!</h1>"); // says Hello
// Echo some selected client's request information
out.println("<p>Request URI: " + request.getRequestURI() + "</p>");
out.println("<p>Protocol: " + request.getProtocol() + "</p>");
out.println("<p>PathInfo: " + request.getPathInfo() + "</p>");
out.println("<p>Remote Address: " + request.getRemoteAddr() + "</p>");
// Generate a random number on "each" request via JDK function Math.random()
out.println("<p>A Random Number: <strong>" + Math.random() + "</strong></p>");
out.println("</body></html>");
out.close(); // Always close the output writer
// For testing and debugging - Print a message to Tomcat's console
System.out.println("hello world, to Tomcat!"); // Check Tomcat's console for this message
}
}

4
5/21/2024

First Web App: Compile & Web.xml


• Compile HelloHome.java
webapps\hello\WEB-INF\classes> javac -cp 'D:\working\Tomcat 8.5\lib\servlet-api.jar' .\HelloServlet.java
webapps\hello\WEB-INF\classes> ls

Directory: D:\working\Tomcat 8.5\webapps\hello\WEB-INF\classes


• Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 5/16/2024 4:40 PM 1718 HelloServlet.class
-a--- 5/16/2024 4:07 PM 1707 HelloServlet.java

• Web.xml
<web-app>
<display-name>Hello WebApp</display-name>
<servlet>
<display-name>HelloServlet</display-name>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/sayHello</url-pattern>
</servlet-mapping>
</web-app>

First Web App: JSP page


• Hello.jsp
<html>
<head>
<title>Hello JSP</title>
<style>
.para {
background-color: yellow;
color: black;
}
.value {
background-color: black;
color: white;
}
</style>
</head>
<body>
<h1>Hello, world!</h1>
<p><span class="para">Request URI: </span><span class="value"><%= request.getRequestURI() %></span></p>
<p><span class="para">Protocol: </span><span class="value">Protocol: <%= request.getProtocol() %></span></p>
<p><span class="para">Path Info: </span><span class="value">PathInfo: <%= request.getPathInfo() %></span></p>
<p><span class="para">Remote Address: </span><span class="value">Remote Address: <%
out.println(request.getRemoteAddr()); %></span></p>
<p><span class="para">A Random Number: </span><span class="value">A Random Number: <strong><%= Math.random()
%></strong></span></p>
</body>
</html>

10

5
5/21/2024

Working with Eclipse

11

Eclipse Tomcat
1. Menu: File / New / Others…
2. Select new Server / Apache / Tomcat 8.5
3. Double click on server Tomcats to config
4. Start server and check log

12

6
5/21/2024

Dynamic Web Project


1. Menu: File / New / Dynamic Web
Project
2. Generate web.xml file
3. New HTML file: HelloHome.html (and
fill the contents)
4. New JSP file: Hello.JSP (and fill the
contents)
5. Menu Run & select Run on Serrver
(Tomcat)
6. Check the browser
1. http://localhost:8080/Hello/HelloHome.html
2. http://localhost:8080/Hello/Hello.jsp

13

Adding servlets
1. New Servlet: HelloServlet
2. Fill the contest (doGet() method)
3. Define Servlet name and mapping
in web.xml
4. Run project (with option restart
server)
5. Servlet is compiled and deployed
to Tomcat.
6. If we change the contents (servlet,
JSP), they are automatically
recompiled and deployed to
Tomcat

14

You might also like