1 Source
https://www.vogella.com/tutorials/JavaWebTerminology/article.html, by Lars Vogel
https://www.javaguides.net/2020/01/java-web-application-tutorial.html, by Ramesh Fadatare
In order to deploy my ECS instance
2 Basic terms
2.1 Web development
- Since Java has strong support for web development, Java is frequently used at the server side.
- Web development: If you develop a web application (independent of the programming language your are using), you typically put your web application on a dedicated server.
For example, blog.vogella.com contains the vogella blog. This blog is a web application powered by WordPress which is a web application written in the server-side scripting language PHP. - Java web or Java EE container: Java web applications are typically not running directly on the server. Java web applications are running inside a web container on the server.
The container provides a runtime environment for Java web applications. JVM (Java Virtual Machine) is for local running Java applications. The container itself runs in the JVM.
Java distinguishes two containers: the web container and the Java EE container. Typical web containers in the Java world are Tomcat or Jetty. A web container supports the execution of Java servlets and Java Server Pages. A Java EE container supports additional functionality, for example, distribution of server load. - Java Web Frameworks: Most are based on servlets. Popular are GWT, JavaServer Faces, Struts and the Spring framework.
2.2 Java Web application
- A Java web application is a collection of dynamic resources (such as Servlets, Java Server Pages, Java classes and jars) and static resources (HTML pages and pictures).
- A Java web application can be deployed as a WAR (Web ARchive) file, a zip file which contains the complete content of the corresponding web application.
2.3 Java Web Standards
- Standard Java technologies are defined via a standard process called the Java Community Process (JCP). The following technologies are defined via the JCP.
- Servlet is Java class which extends “HttpServlet” and answers a HTTP request within a web container. https://www.oracle.com/java/technologies/java-servlet-tec.html.
- JavaServer Page (JSP) are files which contain HTML and Java code. The web cotainer compiles the JSP into a servlet at the first time the JSP is accessed.
- JavaServer Pages Standard Tag Library(JSTL) encapsulates the core functionality common to many Web applications as simple tags.
2.4 Tools and Technology for a Java Web App Dev
- JSP - 2.2 +
- IDE - STS/Eclipse Neon.3
- JDK - 1.8 or later
- Apache Tomcat - 8.5
- JSTL - 1.2.1
- Servlet API - 2.5
- MySQL - mysql-connector-java-8.0.13.jar
2.5 Class Diagram
3 Dev Steps of User Management Web App
3.1. Create an Eclipse Dynamic Web Project
- Eclipse Java EE IDE, select File > New > Dynamic Web Project
- Enter Project name javawebusrmgt
- Tomcat 8.5.71, select path usr/local/tomcat
Here Install tomcat on Ubuntu 20.04 is different from that on Ali ECS Os, google main difference is:
(1) enable .sh to be executed
sudo sh -c ‘chmod +x /usr/local/tomcat/bin/*.sh’
(2) vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat 8.5 servlet container
After=network.target
[Service]
Type=forking
User=www
Group=www
Environment="JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/usr/local/tomcat"
Environment="CATALINA_HOME=/usr/local/tomcat"
Environment="CATALINA_PID=/usr/local/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl status tomcat //check the status
3.2. Add Dependencies
Downloaded from https://github.com/RameshMF/jsp-servlet-jdbc-mysql-crud-tutorial
I wonder where to download all these jar files.
3.3. Project Structure
not same as the source
no WebContent folder and other stuff.
3.4. MySQL Database Setup
- Install mysql-server
sudo apt install mysql-server - secure mysql server
sudo mysql_secure_installation - Connect mysql
mysql -u root -p
ERROR 1698 (28000): Access denied for user ‘root’@‘localhost’
sudo mysql -u root -p
OK
- ? Is there a problem when using JDBC to connect mysql ? How does mysql know the user is root or normal user? (todo)
3.5. Create a JavaBean - User.java
I can do it myself.
3.6. Create a UserDAO.java
Copy and paste.
3.7. Create a UserServlet.java
Copy and paste.
@WebServlet("/") //don't understand here, todo
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1 L;
private UserDAO userDAO;
...
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
try {
switch (action) {
case "/new": //try to systemout some message, but can't see the effect
showNewForm(request, response);
break;
case "/insert":
insertUser(request, response);
break;
3.8. Creating User Listing JSP Page - user-list.jsp
Copy and paste.
3.9. Create a User Form JSP Page - user-form.jsp
Copy and paste.
3.10. Creating Error JSP page
Copy and paste.
3.11. Deploying and Testing the Application Demo
- I export war file from Eclipse. Copy to CATALINA_HOME. Original set /data/wwwroot/default, as Aliyun demos, but browser found 404. At last I deploy war file to /usr/local/tomcat/webapps, then got “Error” and “null”. I have been stuck for a couple of days.
- I changed web.xml like following, but it still doesn’t work:
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.prog4buss.javawebusrmgt.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/WEB-INF/classes/com/prog4buss/javawebusrmgt/web/UserServlet.class</url-pattern>
</servlet-mapping>
- I check environment variables, export CATALINA_HOME, doesn’t work.
- I check Tomcat examples, found that if copy examples directory to /data/wwwroot/default, execute servlet can also experence 404. But when change CATALINA_HOME to /usr/local/tomcat, it is OK. Why?
- Another consideration is whether the problem is JDBC connection, but don’t know how to debug. (todo)
4 Conclusion
What I want is to deploy servlet program to ECS server with Tomcat, JSP, MySQL from scracth. Tomcat and MySQL is OK, and connect MySQL with “sudo mysql -u www -p”. But got stucked in servlet for a couple of days. Come back later.