前言:
前面已经讲了如何配置jdk,Tomcat和Mysql。这次我们来实现以下如何将本地的项目部署到服务器。
前台两个页面:
Index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
买了个服务器 第一次尝试搭建网站
<form action="${pageContext.request.contextPath}/index_servlet" method="GET">
<input type="text" name = "name" />
<input type="submit" value ="跟我去后台看看"/>
</form>
</body>
</html>
Newfile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
哈哈哈哈 !欢迎:${name } !
</body>
</html>
dao层 :
public class TeacherDaoImpl {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet result = null;
// 保存注册信息
public void save(String name) {
String sql = "INSERT INTO teachers(name) value(?)";
try {
connection = jdbcUtil.getConnection();
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, name);
prepareStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
jdbcUtil.close(connection, prepareStatement);
}
}
}
servlet:
TeacherDaoImpl teacherDaoImpl = new TeacherDaoImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
teacherDaoImpl.save(name);
request.setAttribute("name", name);
request.getRequestDispatcher("/NewFile.jsp").forward(request, response);
}
jdbcUtils:
package utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class jdbcUtil {
private static String url = null;
private static String user = null;
private static String password = null;
private static String driverClass = null;
static {
try {
Properties properties = new Properties();
//得到配置文件的流通道
InputStream in = jdbcUtil.class.getResourceAsStream("/db.properties");
properties.load(in);
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
driverClass = properties.getProperty("driverClass");
//注册Driver
Class.forName(driverClass);
} catch (Exception e) {
e.printStackTrace();
System.out.println("驱程程序注册出错");
}
}
//得到连接
public static Connection getConnection() {
try {
Connection cnn = DriverManager.getConnection(url, user, password);
return cnn;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
//关闭连接
public static void close(Connection cnn, Statement st) {
if (st != null) {
try {
st.close();
st = null; //告知系统回收
} catch (Exception e) {
e.printStackTrace();
//将错误转换成RuntimeException运行错误抛出
throw new RuntimeException(e);
}
}
if (cnn != null) {
try {
cnn.close();
cnn = null;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
//关闭连接重载
public static void close(Connection cnn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if (st != null) {
try {
st.close();
rs = null;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if (cnn != null) {
try {
cnn.close();
cnn = null;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
db.properties:
url=jdbc:mysql://localhost:3306/day15?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
user=root
password=123456
driverClass=com.mysql.cj.jdbc.Driver
注意:关键是数据库之间的连接。
还有要把项目打包成war包 我的是:wanindex.war 上传到apache-tomcat-9.0.10的webapps。
当运行一次的时候它自动解压:wanindex wanindex.war
我的还挂载服务器上:
http://codingcoge.cn:8080/wanindex/Index.jsp