Simple use of Eclipse for IDE tools to create a simple Web project

Keywords: Java Eclipse IDE

Previously, it was described that running a Servlet in a simple way is possible for a simple project, but not desirable for complex projects. IDE, an integrated development environment, is designed for large projects, facilitates project management, reduces a lot of unnecessary work for programmers, and focuses more attention on core business. The IDE described here is a special feature of EclipseVersion 2, Eclipse-jee, favors developing Java Web projects.

1) Download eclipse-jee

Downloads on eclipse's official website can be accelerated by switching mirror s in China

I downloaded eclipse-jee-2021-09-R-win32-x86_64, which can be used without installing it.

The first time you open it, you will be asked to select a directory as your workspace.

Configure Tomcat after entering the main interface

Click on Window, select Preferences, find Server, Runtime Environments. add (New Server Runtime Environment), select your local version of Tomcat, select your Tomcat installation directory. jre selects your local jre directory.

File creates a new Dynamic web project, the Content directory corresponds to the ROOT directory in tomcat, but this mapping can be changed later. Remember to check Generate for web.xml to complete.

Create a new index.html as our default page in the webapp directory (note that HTML file names cannot be capitalized)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
Hello, this is the default web page!
</body>
</html>

Be sure to be in webapp, because this is the relationship between webapp and ROOT, while JSP and HTML pages are in the ROOT directory, only servlet s are in the classes directory under WEB-INF.

Before running, select our browser, click Web Browser in Window s to select 1 system default browser

Right-click on the project name run as and select our Tomcat. If 404 is displayed and the web page cannot be found, there are two solutions:

1. Complete URL s

2. Configure index.html in web.xml as the default page. Add an index.html in <welcom-file-list></welcom-file-list>, and you can see that there are two modes below, Design ing and Source, that is, Tables and Source, which you can switch to Source if you are not used to. I always open web.xml with a little red cross and close the file and disappear, so I don't need to.

After creating a simple html, let's try the JSP page

JSP page file names are not as canonical as Servlet s

Create a new JSP file under webapp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello JSP page</title>
</head>
<body>
<%
     out.println("hello,JSP page");
%>
</body>
</html>

Since the default encoding for eclipse is charset=ISO-8859-1, which does not support Chinese, let's modify the encoding for JSP by right-clicking Preference in the JSP code space and selecting encoding Unicode(UTF-8) in the pop-up page.

Similarly, we need to change the encoding of text content to UTF-8 (which is program independent), select the menu bar Windows--preferences, search for Content Type in the search box, change the JSP encoding under Text to UTF-8, and click update;

Finally, let's write another Servlet

Create a new servlet named DisplayHeader in the src/main/java directory

// Import required java Libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
 
// Extending the HttpServlet class
public class Refresh extends HttpServlet {
 
  // Methods for handling GET method requests
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set refresh autoload time to 5 seconds
      response.setIntHeader("Refresh", 5);

      response.setCharacterEncoding("utf-8");
 
      // Set Response Content Type
      response.setContentType("text/html");
 
      // Get current time
      Calendar calendar = new GregorianCalendar();
      String am_pm;
      int hour = calendar.get(Calendar.HOUR);
      int minute = calendar.get(Calendar.MINUTE);
      int second = calendar.get(Calendar.SECOND);
      if(calendar.get(Calendar.AM_PM) == 0)
        am_pm = "AM";
      else
        am_pm = "PM";
 
      String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
    
      PrintWriter out = response.getWriter();
      String title = "auto refresh Header Set up";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +       "transitional//en\">\n";
      out.println(docType +
        "<html>\n" +
        "<head><title>" + title + "</title></head>\n"+
        "<body bgcolor=\"#f0f0f0\">\n" +
        "<h1 align=\"center\">" + title + "</h1>\n" +
        "<p>The current time is:" + CT + "</p>\n");
  }
  // Methods for handling POST method requests
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }
}

Configure web.xml

      <servlet>
        <servlet-name>DisplayHeader</servlet-name>
        <servlet-class>DisplayHeader</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DisplayHeader</servlet-name>
        <url-pattern>/DisplayHeader</url-pattern>
    </servlet-mapping>

Run and complete the url.

Posted by gnize on Fri, 01 Oct 2021 10:16:57 -0700