• 12月10日学习日志


    今天学习了java web学生信息系统的增加操作。

    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="addsuccess" method="post">
        姓名:<input type="text" name="name"><br>
        年龄:<input type="text" name="age"><br>
        学号:<input type="text" name="num"><br>
    <input type="submit" value="提交">
    </form>
    </body>
    </html>

    servlet代码如下:

    package com.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class add
     */
    @WebServlet("/add")
    public class add extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public add() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.getRequestDispatcher("add.jsp").forward(request, response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    package com.servlet;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    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 com.jdbc.jdbcUtil;
    
    /**
     * Servlet implementation class addsuccess
     */
    @WebServlet("/addsuccess")
    public class addsuccess extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public addsuccess() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            try {
                Connection con=jdbcUtil.getcon();
                String sql="insert into  stu(name,age,num) values(?,?,?)";
                PreparedStatement pst=con.prepareStatement(sql);
                pst.setString(1, request.getParameter("name"));
                pst.setInt(2, Integer.parseInt(request.getParameter("age").toString()));
                pst.setString(3, request.getParameter("num"));
                pst.executeUpdate();
                jdbcUtil.close(con, pst, null);
                response.sendRedirect(request.getContextPath()+"/list");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    【斜率DP】BZOJ 1010:玩具装箱
    【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
    网络流24题 (一)
    关于ax+by=c的解x,y的min(|x|+|y|)值问题
    【概率】COGS 1487:麻球繁衍
    【概率】poj 2096:Collecting Bugs
    [洛谷P5376] 过河卒二
    [TJOI2019] 洛谷P5339 唱、跳、rap和篮球
    [洛谷P3851] TJOI2007 脱险
    [洛谷P3843] TJOI2007 迷路
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14228645.html
Copyright © 2020-2023  润新知