servlet通过JavaBean传值给jsp,连接数据库实现增加功能
通过jsp页面给toob表添加一条记录
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.cn;
/**
*
* @author 86187
*/
public class Student {
private int id;
private String stuno;
private String className;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the stuno
*/
public String getStuno() {
return stuno;
}
/**
* @param stuno the stuno to set
*/
public void setStuno(String stuno) {
this.stuno = stuno;
}
/**
* @return the className
*/
public String getClassName() {
return className;
}
/**
* @param className the className to set
*/
public void setClassName(String className) {
this.className = className;
}
}
<%--
Document : insetjsp
Created on : 2021-6-21, 15:21:09
Author : 86187 ${messsage}
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="./insertServlet" method="post">
<input type="text" name="id" value="<%=request.getAttribute("message")%>"/>
<input type="text" name="stuno" value="<%=request.getAttribute("message")%>"/>
<input type="text" name="className" value="<%=request.getAttribute("message")%>"/>
<input type="submit" value="添加"/>
</form>
</body>
</html>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import com.cn.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author 86187
*/
@WebServlet(name = "insertServlet", urlPatterns = {"/insertServlet"})
public class insertServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String id=request.getParameter("id");
String stuno=request.getParameter("stuno");
String className=request.getParameter("className");
Connection conn=null;
PreparedStatement pstmt=null;
int num=0;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3396/01db", "root", "123456");
String sqlstr="insert into Toob values(?,?,?)";
pstmt=conn.prepareStatement(sqlstr);
pstmt.setInt(1,Integer.parseInt(id));
pstmt.setString(2,stuno);
pstmt.setString(3,className);
num=pstmt.executeUpdate();
} catch (ClassNotFoundException ex) {
Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
}finally{
if (pstmt!=null){
try{pstmt.close();
} catch (SQLException ex) {
Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (conn!=null){
try{conn.close();
}catch (SQLException ex) {
Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
if(num>0){
request.setAttribute("message","插入成功");
}else
{
HttpSession session=request.getSession();
session.setAttribute("message","插入不成功");
response.sendRedirect("./insertjsp.jsp");
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}