1. Create a new java project first, select web application
2. Fill in the project name and path
3. After the project is built, create two folders named classes and lib in WEB-INF
4. Then click File——>Project Structure——>Modules——>Paths to modify the paths of Output path and Test output path
5. Click Add Configuration, click the plus sign, find Tomcat, select local
6. View your Tomcat console code and modify the configuration
Return to idea, the parameter configuration is as follows:
A warning appears below, click fix, and the input name is recommended to be the same as the name of the project you built. apply.
At this time, return to the server page and configure according to the following figure. Your Tomcat version does not need to be the same as mine. My tomcat port has been changed to 8100. The default is 8080.
The page becomes like this at this time:
Next select File——>Project Structure——>Modules——>Dependencies——>+——>Libraries, select Tomcat 9.0.1 under Application Server Libraries, click Add Selected, so that you can import the JSP and servlet jar Pack it. I also added my own jar files such as struct2, mysql-jdbc driver.
7. Test
Create a new package named "cn.jxs.servlet" under the src folder, and create a new file named HelloServlet.java under the package
7.1. Change the web.xml file under WEB-INF:
Original file:
Next we start to test whether the creation is successful:
Click:
The following console displays the log:
then jump to the browser page
Next, enter the web page address we just configured by ourselves: http://localhost:8100/javaEE_war_exploded/aa and
I saw the content written by HelloServlet.java.
Everything is currently normal. Next, create the corresponding jdbc-bean:
8. Test jdbcbean.
1. Create a JSP file:
create a WebRoot directory under the WEB directory, and then create 4 jsp files in this directory:
login.jsp:
<%--
Created by IntelliJ IDEA.
User: 86158
Date: 2020/9/21
Time: 10:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
<style type="text/css">
#myform{
margin: 0 auto;
text-align: center;
}
#myform div{
text-align: center;
}
</style>
</head>
<body style="align-content: center">
<form id="myform" action="webRoot/validate.jsp" method="post">
<fieldset>
<legend align="center">登陆参数</legend>
<div>
用户名:
<label>
<input type="text" name="username">
</label><br>
密 码:
<label>
<input type="password" name="password">
</label><br>
</div>
<div>
<input type="submit" value="提交">
</div>
</fieldset>
</form>
</body>
</html>
validate.jsp:
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %><%--
Created by IntelliJ IDEA.
User: 86158
Date: 2020/9/21
Time: 10:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="MySqlBean" scope="page" class="org.easybooks.bookstore.jdbc.MySQLConnBean"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>判断界面</title>
</head>
<body>
<%
String user = request.getParameter("username");
String password = request.getParameter("password");
boolean isValid = false;
String sql = "select username,password from user";
MySqlBean.OpenConn();
ResultSet res = MySqlBean.executeQuery(sql);
try{
while (res.next()){
try {
if(res.getString("username").equals(user)&&res.getString("password").equals(password)){
isValid = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}catch (SQLException e){
e.printStackTrace();
}
MySqlBean.close();
if(isValid){
%>
<jsp:forward page="welcome.jsp"/><%--必须手动关闭 标签--%>
<%
}else{
%>
<jsp:forward page="error.jsp"/>
<%
}
%>
</body>
</html>
welcome.jsp:
<%--
Created by IntelliJ IDEA.
User: 86158
Date: 2020/9/21
Time: 9:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>登陆成功</title>
</head>
<body style="background-color: red">
<%out.print(request.getParameter("username"));%>,您好,欢迎光临叮当书店!
</body>
</html>
error.jsp
<%--
Created by IntelliJ IDEA.
User: 86158
Date: 2020/9/21
Time: 9:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆失败页面</title>
<script type="text/javascript">
window.alert("登陆失败,请检查密码和用户名!")
</script>
</head>
<body>
</body>
</html>
2. Put the mysql-jdbc driver in the lib directory of the web:
9. Test:
1. Modify the web.xml file, change the project startup file to login.jsp, deploy and start the tomcat server.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--这里默认是没有写的,因为默认是index.html-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>cn.jxs.servlet.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/aa</url-pattern>
</servlet-mapping>
</web-app>
Then put the login.jsp file under the web directory:
2. Restart the project server, but the browser input: http://localhost:8100/javaEE_war_explore/