jsp+servlet+jdbc实现表单提交

时间:2024-04-26 17:38:28

1.新建一个maven工程,选webapp模板

jsp+servlet+jdbc实现表单提交

2.安装tomcat

https://tomcat.apache.org/download-80.cgi 下载解压到自定义目录上

ps:在全局变量加上JAVA_HOME变量指向JDK(例如:D:\Java\v1.8),tomcat才能正常运行

3.eclipse配置server

Windows-->Preferences-->Server-->Runtime Environments

添加tomcat

jsp+servlet+jdbc实现表单提交

4.增加tomcat的libary

工程右击--->Build Path --->Configure Build path

选择刚刚加到eclipse的tomcat

jsp+servlet+jdbc实现表单提交

5.编写jsp

jsp+servlet+jdbc实现表单提交

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/saveUserInfo" method="GET">
username:<input type="text" name="username"></input><br/>
adress:<input type="text" name="address"></input>
<input type="submit" value="保存"/>
</form>
</body>
</html>

6.编写映射到servlet接口类的配置文件,web.xml

jsp+servlet+jdbc实现表单提交

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>amie.servlet.helloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/saveUserInfo</url-pattern>
</servlet-mapping>
</web-app>

7.编写servlet类

从上面可以得到,tomcat服务器的路径“/saveUserInfo”已经映射到“amie.servlet.helloServlet”这个类上了

jsp+servlet+jdbc实现表单提交

package amie.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import jdbc.jdbc; public class helloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("gbk");
String username = new String(request.getParameter("username").getBytes("UTF8"));
String address = new String(request.getParameter("address").getBytes("UTF8"));
jdbc mj = new jdbc();
try {
mj.insert(username, address);
response.getWriter().println("写入成功");
} catch (Exception e) {
response.getWriter().print(e);
}
}
}

doGet、doPost等方法会对应路径“/saveUserInfo”的GET、POST的方法

8.编写jdbc

我们上面用了一个jdbc.jdbc类的insert方法

jsp+servlet+jdbc实现表单提交

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; public class jdbc {
public void insert(String name, String address) throws Exception{
String url = "jdbc:mysql://localhost:3306/20171113";
String userName = "root";
String password = "";
Connection conn = DriverManager.getConnection(url,userName,password);
String sql = "INSERT INTO user(username,address) VALUES(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, address);
ps.execute();
conn.close();
}
}

这里值得一提的是:jdbc的mysql驱动(mysql-connector-java-x.x.x-bin)一定要加在jre\lib\ext下,因为此时已经运行在tomcat上脱离了eclipse,runtime的依赖包一定要到java的安装目录下获取。

9.运行

项目右击,run as --> run on server

jsp+servlet+jdbc实现表单提交

jsp+servlet+jdbc实现表单提交

保存后

jsp+servlet+jdbc实现表单提交

数据库也成功存入数据

jsp+servlet+jdbc实现表单提交