一、创建项目
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/n/271912.html
微信扫一扫
支付宝扫一扫