栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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:
	
       UserServlet
       com.prog4buss.javawebusrmgt.web.UserServlet
   
   
   
       UserServlet
       /WEB-INF/classes/com/prog4buss/javawebusrmgt/web/UserServlet.class
   
  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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292438.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号