jsp+servlet+jdbc實現表單提交

1.新建一個maven工程,選webapp模板html

2.安裝tomcatjava

https://tomcat.apache.org/download-80.cgi 下載解壓到自定義目錄上mysql

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

3.eclipse配置serversql

Windows-->Preferences-->Server-->Runtime Environments數據庫

添加tomcatapache

4.增長tomcat的libarytomcat

工程右擊--->Build Path --->Configure Build path服務器

選擇剛剛加到eclipse的tomcatapp

5.編寫jsp

<%@ 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

<?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」這個類上了

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方法

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

保存後

數據庫也成功存入數據

相關文章
相關標籤/搜索