Java Web Application Tutorial (not success, to do later)20210925

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

  1. Since Java has strong support for web development, Java is frequently used at the server side.
  2. 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.
  3. 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.
  4. Java Web Frameworks: Most are based on servlets. Popular are GWT, JavaServer Faces, Struts and the Spring framework.

2.2 Java Web application

  1. 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).
  2. 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

  1. Standard Java technologies are defined via a standard process called the Java Community Process (JCP). The following technologies are defined via the JCP.
  2. 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.
  3. 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.
  4. 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

  1. Eclipse Java EE IDE, select File > New > Dynamic Web Project
  2. Enter Project name javawebusrmgt
  3. 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

  1. Install mysql-server
    sudo apt install mysql-server
  2. secure mysql server
    sudo mysql_secure_installation
  3. Connect mysql
    mysql -u root -p

ERROR 1698 (28000): Access denied for user ‘root’@‘localhost’
sudo mysql -u root -p
OK

  1. ? 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

  1. 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.
    在这里插入图片描述
  2. 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>
  1. I check environment variables, export CATALINA_HOME, doesn’t work.
  2. 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?
  3. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值