Idea創建JavaWeb項目

一、創建項目

1、打開Idea,選擇Create New Project

2、選擇Java Enterprise,然後選擇Web Application

3、填寫項目名稱和項目位置,點擊Next

4、選擇Java SDK版本和容器,例如Tomcat,點擊Next

5、選擇Web Application技術,例如Spring MVC,點擊Next

6、填寫Web Module名稱和路徑,點擊Finish

二、目錄結構

1、src目錄下存放Java源代碼

/src
|--main
|   |--java
|   |   |--包名
|   |       |--*.java
|   |--resources
|       |--*.xml
|--test
    |--java
        |--包名
            |--*Test.java

2、web目錄下存放Web資源和配置文件

/webapp
|--WEB-INF
|   |--classes
|   |   |--*.class
|   |   |--*.properties
|   |--lib
|       |--*.jar
|--*.html
|--*.jsp
|--*.css
|--*.js

三、配置文件

1、web.xml

Web應用程序配置文件,配置Servlet、過濾器、監聽器等Web組件。

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-name>MyWebApp</display-name>
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.example.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.example.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/user/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <listener>
        <listener-class>com.example.StartupListener</listener-class>
    </listener>
</web-app>

2、applicationContext.xml

Spring配置文件,配置Bean、AOP、事務等Spring組件。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    <bean id="userService" class="com.example.UserService">
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean id="userDao" class="com.example.UserDao"/>
</beans>

四、例子代碼

1、HelloServlet

返回Hello World!

package com.example;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<head><title>Hello Servlet</title></head>");
        out.println("<body><h1>Hello World!</h1></body>");
        out.println("</html>");
    }
}

2、LoginFilter

檢查用戶是否登錄,未登錄則跳轉到登錄頁面。

package com.example;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {

    private FilterConfig config;

    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        if (request.getSession().getAttribute("user") == null) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        this.config = null;
    }
}

3、StartupListener

應用程序啟動時初始化Spring容器。

package com.example;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class StartupListener implements ServletContextListener {

    private ClassPathXmlApplicationContext context;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        context.close();
    }
}

五、總結

本文介紹了如何使用Idea創建JavaWeb項目,包括項目創建、目錄結構、配置文件和例子代碼等方面。通過本文的學習,讀者將了解創建JavaWeb項目的流程和相關技術。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/271912.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-17 00:07
下一篇 2024-12-17 00:07

相關推薦

發表回復

登錄後才能評論