Java Web Application①

[日本語]

This is about my experience and also a note that summarizes what I learned from workplace and self-study. It would be appreciated if you could comment your suggestions for any issues on the post.

Other reference materials:
・Zukai De Wakaru Web App. No Subete(Japanese Book)

I am learning about web applications based on Java. Last time, “Hello World” was successfully displayed on the Eclipse console. This time, I succeeded in displaying “Hello Web World” on the browser using Apache Tomcat. I want to share here.

Apache Tomcat should be installed. Check Development Environment Setup.

Workflow:

1.Configure server in Eclipse

1.1 Open the “Servers” view and click “No servers are available. Click this link to~.
PracticingJavaWebApp01Eng001

1.2 Select “Define a New Server”> “Apache”> “Tomcat v9.0 server” and click “Next”.
PracticingJavaWebApp01Eng002

1.3 Enter “C:/tomcat9” in “Tomcat installation directory” and click “Finish”.
PracticingJavaWebApp01Eng003

1.4 Check the following three points.
・ Existence of “Tomcat on localhost” in “Server” view
・ Display “Servers” in Project Explorer
・ The existence of “~/workspace/Servers” in Explorer
PracticingJavaWebApp01Eng004

1.5 In the “Server” view, right-click “Tomcat v9.0 Server at localhost~” and click “Start”.
※If an important Windows Security Alerts is displayed, click “Allow access”.
PracticingJavaWebApp01Eng005

1.6 If “[Started, Synchronized]” is displayed, the server setup was successful.
※To stop the server, right-click “Tomcat v9.0 Server at localhost~” in the “Server” view, click “Stop”, and if “[Stopped, synchronized]” is displayed, the server was successfully stopped.
PracticingJavaWebApp01Eng006

2. Create a dynamic Web project

2.1 Start eclipse and click “File”> “New”> “Dynamic Web Project”.
PracticingJavaWebApp01Eng007

2.2 Enter “testing_tomcat” in the project name and click “Finish”.
PracticingJavaWebApp01Eng008

2.3 When the project is successfully created, “testing_tomcat” is displayed on the screen.
PracticingJavaWebApp01Eng009

3. Add the dynamic web project to the server

3.1 In the “Servers” view, right-click “Tomcat v9.0 Server at localhost~” and click “Stop”.
※Refer to step 1.6

3.2 In the “Servers” view, right-click “Tomcat v9.0 Server at localhost~” and select “Add and Remove…”.
PracticingJavaWebApp01Eng010
3.3 Select “tomcat_test” and click “Add”.
PracticingJavaWebApp01Eng011

3.4 Click “Finish”.
PracticingJavaWebApp01Eng012

3.5 “Server” view Success if “testing_tomcat” is displayed under “Tomcat v9.0 Server at localhost~t”
※Check “server.xml” (“tomcat_test” is described. about line 152)
PracticingJavaWebApp01Eng013

4. Create package and class (Web project)

4.1 Right-click on “testing_tomcat” and select “New”-> “Class”.
PracticingJavaWebApp01Eng014

4.2 Enter “pakete.tomcattest” in the package name, set the name to “HelloWebWorld” and click “Finish”.
PracticingJavaWebApp01Eng015

4.3 Enter the following source code and save it with “cltr + s” on the keyboard.
Source code

package pakete.tomcattest;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWebWorld extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException{

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Hello Web World!</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Hello Web World!</h1>");
		out.println("</body>");
		out.println("</html>");
        }
}

※About source code:
Creating HTML in Java, then processed by the servlet’s doGet method, and it will displayed on the browser.
PracticingJavaWebApp01Eng016

4.3 Check folders and files

"~/Workspace/testing_tomcat/src/pakete/tomcattest/HelloWebWorld.java"
"~/Workspace/testing_tomcat/build/classes/pakete/tomcattest/HelloWebWorld.class"

PracticingJavaWebApp01Eng017

5. web.xml settings (Web project)

5.1 Right-click “WebContent”-> “WEB-INF” and select “New”-> “File”.
PracticingJavaWebApp01Eng018

5.2 Enter “web.xml” in the file name and click “Finish”.
PracticingJavaWebApp01Eng019

5.3 Enter the following in “web.xml” and save it with “cltr + s” on the keyboard.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

    <servlet>
        <servlet-name>hwwservletname</servlet-name>
        <servlet-class>pakete.tomcattest.HelloWebWorld</servlet-class>
    </servlet>

    <servlet-mapping>

        <servlet-name>
            hwwservletname
        </servlet-name>

        <url-pattern>
            /hwwurlpattern
        </url-pattern>

    </servlet-mapping>
</web-app>

※About tags:
<servlet-name> → Describe the servlet name
<servlet-class> → Describe Java class
<url-pattern> → Describe the URL that the user sees. ※The Java class described in <servlet-class> is executed with this URL.
PracticingJavaWebApp01Eng020

6. Check

6.1 Click the “Start” and start the server in the “Servers” view.
PracticingJavaWebApp01Eng021

6.2 Start the browser and access the following URL.

http://localhost:8080/testing_tomcat/hwwurlpattern

6.3 Success if “Hello Web World!” Is displayed as shown below.
PracticingJavaWebApp01Eng022

※If “HTTP status 404? Not found” is displayed, check the following.
・ Step 3. Add the created dynamic web project to the server.
・ Check if “Build Automatically” is checked.
PracticingJavaWebApp01Eng023

 

 

One thought on “Java Web Application①

Leave a comment