用idea,maven和tomcat搭建一个简单web
web的知识概述
- 在tomcat服务器中,存放的资源有后端的以及用来展示数据的.jsp文件
- 在后端,用mvc及分层思想去实现
m:指model,在这里是Java的实体类
c:control,在这里是servlet类
v:view,这里是指显示数据的jsp
正式搭建web
前提
idea已集成好了maven和tomcat
搭建web项目
1.创建maven空项目
2.配置web所需的jar包
3.新建web.xml文件
4.部署到tomcat
撸代码
login.jsp
<%--
Created by IntelliJ IDEA.
User: go
Date: 2020/7/29
Time: 15:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/loginServlet" method="post">
<table>
<tr>
<td>username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp
<%--
Created by IntelliJ IDEA.
User: go
Date: 2020/7/29
Time: 15:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
welcome
</body>
</html>
LoginServlet
package com.lxj.web.servlet;
import com.lxj.domain.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User(username, password);
//2.调用业务逻辑,这里就在servlet直接写了
String path = "/login.jsp";
if(user.getUsername().equals("admin") && user.getPassword().equals("123456")){
path = "/welcome.jsp";
}
//3.分发转向
response.sendRedirect(request.getContextPath()+path);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.lxj.web.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
User
package com.lxj.domain;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
效果