jsp+mysql+servlet

addProduct.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>亲子游戏系统->增加游戏title>
    head>
    <body bgcolor="CCCFFF">
        <hr noshade>
      <div align="center"><%--div 元素的 "align" 属性不被赞成使用--%>
      <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
          <tr>
                <td width="30%">
                    增加游戏
                td>
                <td width="30%">
                    <a href="http://localhost:8080/familys/product/r_lookProduct.jsp">查看游戏a>
                td>
                <td width="30%">
                    <a href="http://localhost:8080/familys/product/updateProduct.jsp">修改游戏a>
                td>
                <td width="30%">
                    <a href="http://localhost:8080/familys/product/deleteProduct.jsp">删除游戏a>
                td>
          tr>
      table>
      div>
      <hr noshade>
      <br><br>
      <form action="http://localhost:8080/familys/AddProductServlet" method="post">
          <table border="5" cellspacing="0" cellpadding="0" width="60%" align="center">
                <tr>
                     <td height="30" width="50%" align="right">编号td>
                     <td width="50%"><input type="text" size="30" name="id" value="">td>
                tr>
                tr>
                <tr>
                     <td height="30" width="50%" align="right">游戏名称td>
                     <td width="50%"><input type="text" size="30" name="name" value="">td>
                tr>
                <tr>
                     <td height="30" width="50%" align="right">地址td>
                     <td width="50%"><input type="text" size="30" name="addresss" value="">td>
                tr>
                <tr>
                     <td height="30" width="50%" align="right">商家电话td>
                     <td width="50%"><input type="text" size="30" name="phone" value="">td>
                tr>
                <tr>
                     <td height="30" width="50%" align="right">商家名称td>
                     <td width="50%"><input type="text" size="30" name="business" value="">td>
                tr>
                <tr>
                     <td height="30" width="50%" align="right">活动时间td>
                     <td width="50%"><input type="text" size="30" name="begintime" value="">td>
                tr>
                <tr>
                     <td height="30" width="50%" align="right">价格/人td>
                     <td width="50%"><input type="text" size="30" name="price" value="">td>
                tr>
                <tr>
                    <td colspan="2" align="center"><%--单元格可以横跨的列数--%>
                        <input type="submit" value="确 定" size="12">     
                        <input type="reset" value="清 除" size="12">
                    td>
                tr>
            table>
        form>
    body>
html>



AddProductServlet.java

/*
 * 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 productManager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import lookMessage.LookMessageBean;

/**
 *
 * @author 17678
 */
public class AddProductServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String id=new String(request.getParameter("id").getBytes("ISO-8859-1"),"UTF-8");
        String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String address=new String(request.getParameter("address").getBytes("ISO-8859-1"),"UTF-8");
        String phone=new String(request.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
        String business=new String(request.getParameter("business").getBytes("ISO-8859-1"),"UTF-8");
        String begintime=new String(request.getParameter("begintime").getBytes("ISO-8859-1"),"UTF-8");
        String price=new String(request.getParameter("price").getBytes("ISO-8859-1"),"UTF-8");
        if(id.length()==0||name.length()==0||address.length()==0||phone.length()==0||business.length()==0||begintime.length()==0||price.length()==0){
            response.sendRedirect("http://localhost:8080/familys/product/addProduct.jsp");
        }else{
            try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/family?useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","1234");
                stmt=con.createStatement();
                HttpSession session=request.getSession();
                String sql1="select * from product where id='"+id+"'";
                rs=stmt.executeQuery(sql1);
                rs.last();
                int k;
                k=rs.getRow();
                if(k>0){
                    response.sendRedirect("http://localhost:8080/familys/product/addProduct.jsp");
                }else{
                    String sql2="insert into product"+"(id,name,address,phone,business,begintime,price)"+"values("+"'"+id+"'"+","+"'"+name+"'"+","+"'"+address+"'"+","+"'"+phone+"'"+","+"'"+business+"'"+","+"'"+begintime+"'"+","+"'"+price+"'"+")";
                    stmt.executeUpdate(sql2);
                }
                String sql3="select * from product";
                rs=stmt.executeQuery(sql3);
                ArrayList prolist=null;
                prolist=new ArrayList();
                while(rs.next()){
                    LookProductBean ff=new LookProductBean();
                    ff.setId(rs.getString("id"));
                    ff.setName(rs.getString("name"));
                    ff.setAddress(rs.getString("address"));
                    ff.setPhone(rs.getString("phone"));
                    ff.setBusiness(rs.getString("business"));
                    ff.setBegintime(rs.getString("begintime"));
                    ff.setPrice(rs.getString("price"));
                    prolist.add(ff);
                    session.setAttribute("prolist", prolist);
                }
                rs.close();
                stmt.close();
                con.close();
                response.sendRedirect("http://localhost:8080/familys/product/r_lookProduct.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

}


mysql

img

jsp页面点确定后至今HTTP 500 错在哪怎么改

img

img

空指针异常了,后端加断点看看哪里问题,或者捕获下异常,看看控制台有没有错误提示

img