王座 2021-11-07 16:23 采纳率: 100%
浏览 64
已结题

jsp页面点击提交后跳转到studentservlet的页面就报500错误。

img

Stuentservlet.java代码如下


package com.y;

import javax.servlet.RequestDispatcher;
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 java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "addStudentServlet",urlPatterns = {"/student-action"})
public class StudentServlet extends HttpServlet {
    private static final long serialVersionUID=1l;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action=request.getParameter("action");
if (action!=null&&action.equals("addStudent"))
{ addStudent(request,response);}
else if (action!=null&&action.equals("remove")){
    removeStudent(request,response);
}else{
    listStudent(request,response);
} }
public void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id=Integer.parseInt(request.getParameter("id"));
String name=new String(request.getParameter("name").getBytes("iso-8859-1"),"UTF-8");
int age=Integer.parseInt(request.getParameter("age"));
    String major=new String(request.getParameter("major").getBytes("iso-8859-1"),"UTF-8");
    Student s=new Student();
    s.setSno(id);
    s.setAge(age);
    s.setMajor(major);
    s.setName(name);
      StudentDao dao=new StudentDaoImpl();
      try{
          boolean success=dao.addStudent(s);
          if (success){
              String message="插入记录成功";
              request.setAttribute("msg",message);
              listStudent(request,response);
          }else {
              String message="插入记录失败";
              request.setAttribute("msg",message);
              RequestDispatcher rd=request.getRequestDispatcher("manageStudentInfo.jsp");
              rd.forward(request,response);
          }
}catch (SQLException e){
          e.printStackTrace();
}}
public void listStudent (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    StudentDao dao=new StudentDaoImpl();
    try {
        List<Student> list = dao.listStudent();
        request.setAttribute("studentList", list); }
    catch (SQLException e){
        e.printStackTrace(); }
    RequestDispatcher rd=request.getRequestDispatcher("manageStudentInfo.jsp");
    rd.forward(request,response);
}
    public void removeStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentDao dao=new StudentDaoImpl();
    }
    }


package com.y;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    public boolean addStudent(Student s)throws SQLException{
        Connection conn=getConnection();
        String sql="INSERT INTO students(id,name,age,major)VALUES(?,?,?,?)";
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,s.getSno());
            pstmt.setString(2,s.getName());
            pstmt.setInt(3,s.getAge());
            pstmt.setString(4,s.getMajor());
            pstmt.executeUpdate();
            return true;
        }catch (SQLException sqle){
            System.out.println(sqle);
            return false;
        } }

        public List<Student> listStudent()throws SQLException{
        Connection conn=getConnection();
        String sql="SELECT *FROM students";
        List<Student> list=new ArrayList<Student>();
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String name=rs.getString(2);
                int age=rs.getInt(3);
                String major=rs.getString(4);
                Student s=new Student();
                s.setSno(id);
                s.setName(name);
                s.setAge(age);
                s.setMajor(major);
                list.add(s);
            }
            return list;
        }catch (SQLException sqle){
            System.out.println(sqle);
            return null;
        } }
        public int removeStudent(int id)throws SQLException
        {

return 1;
        }
}

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-11-07 16:47
    关注

    空指针异常,Dao层22行。
    数据库配置不对,导致获取不到连接对象。
    你的url写错了哈,是jdbc,你写成了jabc

    url="jdbc:mysql://localhost:3306/stuInfoManage?serverTimezone=UTC"
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 创建了问题 11月7日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。