package com.ftz.Demo.Servlet;
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;
/** * @author ${范涛之} * @Description * @create 2021-12-06 23:44 */
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter(); //响应流
writer.print("Hello,Serlvet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>com.ftz.Demo.Servlet.Servlet1</servlet-class>
</servlet>
<!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/project/hello</url-pattern>
</servlet-mapping>
</web-app>
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
package com.ftz.Demo.Servlet;
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;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/** * @author ${范涛之} * @Description * @create 2021-12-07 1:51 */
public class LoginUp extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置服务器端以UTF-8编码进行输出
resp.setCharacterEncoding("UTF-8");
//设置浏览器以UTF-8编码进行接收,解决中文乱码问题
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
/** * 获取前端传来的值 */
String username = req.getParameter("username");
String passwd = req.getParameter("passwd");
String url = "jdbc:mysql://localhost:3306/servletstudy";
String name = "root";
String pwd = "root";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url,name,pwd);
String sql = "insert into user \n"+
"values\n" +
"(1,"+username+','+passwd+");";
;
Statement statement =connection.createStatement();
// 执行sql语句
int temp = statement.executeUpdate(sql);
System.out.println("返回的值:"+temp);
statement.close();
connection.close();
resp.sendRedirect("/success.jsp");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>com.ftz.Demo.Servlet.Servlet1</servlet-class>
</servlet>
<!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>World</servlet-name>
<servlet-class>com.ftz.Demo.Servlet.Servlet2</servlet-class>
</servlet>
<!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>World</servlet-name>
<url-pattern>/world</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Loginup</servlet-name>
<servlet-class>com.ftz.Demo.Servlet.LoginUp</servlet-class>
</servlet>
<!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>Loginup</servlet-name>
<url-pattern>/LoginUp/Register</url-pattern>
</servlet-mapping>
</web-app>
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121759123
内容来源于网络,如有侵权,请联系作者删除!