今天学习了servlet的使用,实现了数据的添加和更新,但是遇到了404和500的问题,发现404 的我问题可以在jsp文件中的<form action="/StudentBiz" method="get">加一个”/“就可以解决,但是500的错误解决不掉。呜呜呜~
代码如下:
package dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import bean.Student;
import util.JDBC;
public class StudentDao {
public boolean insertStudent(Student stu) {
boolean res=false;
String sql="INSERT student1(`no`,`name`,`age`,`department`,`sex`) VALUES(?,?,?,?,?)";
Object[] obj= {
stu.getNo(),stu.getName(),stu.getAge(),stu.getDepartment(),stu.getSex()
};
int n=JDBC.update(sql, obj);
if(n>0) {
res=true;
}
return res;
}
public List<Student> getAllStus(){
List<Student> list=null;
QueryRunner qr=new QueryRunner();
ResultSetHandler rs=new BeanListHandler<Student>(Student.class);
String sql="SELECT * FROM `student1`";
try {
list=(List<Student>) qr.query(JDBC.getConn(),sql, rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
Student stu=new Student("142", "ixuan", "男", 20, "软件工程");
StudentDao studao=new StudentDao();
// if(studao.insertStudent(stu)) {
//System.out.println("插入成功");
// }
List<Student> stulist =studao.getAllStus();
for (Student student : stulist) {
System.out.println(student.toString());
System.out.println("");
}
}
}
package app;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import org.apache.catalina.connector.Response;
import bean.Student;
import dao.StudentDao;
/**
* Servlet implementation class StudentBiz
*/
@WebServlet("/StudentBiz")
public class StudentBiz extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentBiz() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.print("大家好");
//String no= request.getParameter("no");
}
StudentDao stud=new StudentDao();
public List<Student> showAllStus(){
List<Student> list=null;
list=stud.getAllStus();
return list;
}
public boolean addStudent(Student stu) {
boolean res=false;
res=stud.insertStudent(stu);
return res;
//return stud.insertStudent(stu);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
经过学习,发现自己的配置的包不全,有的地方还没解决,加油!!!!!!
标签:21,记录,2023.02,List,response,stu,Student,import,public From: https://www.cnblogs.com/xiao-hong111/p/17142594.html