问题解决 —— Http Status 500

版权声明:感觉我写的还算不错的的话希望你能够动动你的鼠标和键盘为我点上一个赞或是为我奉献上一个评论,在下感激不尽!_______________________________________________________欢迎转载,希望在你转载的同时,添加原文地址,谢谢配合 https://blog.csdn.net/fonxian/article/details/46697871

在完成JavaWeb项目的过程中经常会出现这种错误

500:指的是服务器内部错误

通常遇到这种情况,我会去看控制台的信息,那里肯定会有提示,空指针(值没有传过去),或者java.sql.SQLException,在界面和Dao

中好好检查一下,就可以找到问题所在。

可以在中间插入一些测试语句,逐渐缩小范围,就可以找到错误是在哪一步中出现的了。

出现错误的可能

  1. 使用for循环,使得对象越界访问

    例如:
    //若只有5个元素,当访问到第6个了,就属于越界访问,就会报错
    for(i = 0;i<10;i++)
    Student stu = dao.load(i);

  2. 数据库出现问题,没有连接,或读取不出数据,造成某些变量为空
  3. 发送的变量名和接收的变量名不一致

HTTP Status 500 -

12-30

type Exception reportrnrnmessage rnrndescription The server encountered an internal error () that prevented it from fulfilling this request.rnrnexception rnrnorg.apache.jasper.JasperException: Unable to compile class for JSPrnrnGenerated servlet error:rnD:\Tomcat5.0\work\Catalina\localhost\land\org\apache\jsp\useradd_jsp.java:6: cannot access LanduserBLL.AdduserBLLrnrnrnGenerated servlet error:rnbad class file: D:\Tomcat5.0\webapps\land\WEB-INF\classes\LanduserBLL\AdduserBLL.classrnclass file has wrong version 49.0, should be 48.0rnPlease remove or make sure it appears in the correct subdirectory of the classpath.rnimport LanduserBLL.AdduserBLL;rn ^rn1 errorrnrnrnrn org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)rn org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)rn org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)rn org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rnrnrnnote The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.rnrn

http status 500 java.lang.NullPointerException

03-01

HTTP Status 500 - java.lang.NullPointerExceptionrnrnrntype Exception reportrnrnmessage java.lang.NullPointerExceptionrnrndescription The server encountered an internal error that prevented it from fulfilling this request.rnrnexception rnorg.apache.jasper.JasperException: java.lang.NullPointerExceptionrn org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:723)rnrnrnrnroot cause rnjava.lang.NullPointerExceptionrn hospital.Patient.checkUser(Patient.java:321)rn hospital.DBOperation.(DBOperation.java:31)rn hospital.Patient.(Patient.java:29)rn org.apache.jsp.patient.login_jsp._jspService(login_jsp.java:134)rn org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:723)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:723)rnrnrnrnnote The full stack trace of the root cause is available in the Apache Tomcat/6.0.41 logs.rnrnrnApache Tomcat/6.0.41rnrnrnrnrnrnrn提到的错误地方的代码rnrnrnrnpublic static Connection getConnection()rn tryrn if(conn == null)rn Class.forName("com.mysql.jdbc.Driver");rn conn=DriverManager.getConnection("jdbc:mysql://pc-win8/hospital","root","1234");rn Debug.log("Connecion created.");rn rnrn elsern Statement stmt=conn.createStatement();rn ResultSet rs=stmt.executeQuery("SELECT COUNT(*) FROM administrator");rn if(rs==null||!rs.next())rn Class.forName("com.mysql.jdbc.Driver");rn conn=DriverManager.getConnection("jdbc:mysql://pc-win8/hospital","root","1234");rn Debug.log("Connecion re-created.");rn rnrn rn rn catch(Exception ex)rn Debug.log(Debug.getExceptionMsg(ex));rn rn finallyrn return conn;rn rn rnrnrnrnpublic class Patientrn extends DBOperationrn public static final String NAME="Name";rn public static final String SECTION="Section";rn public static final String ANONYMOUS="anonymous";rnrn private int PID;rnrn /**rn * 构造器rn *rn * 参数:rn * id-用户名rn */rn public Patient(String id)rn throws InvalidUserExceptionrn super(id);rn rnrnrnrnrnprotected void checkUser(String id)rn throws InvalidUserExceptionrn conn=DBConnection.getConnection();rnrn strSQL="SELECT Username FROM patient WHERE Username='" + id + "'";rnrn if(id.equals(Patient.ANONYMOUS))rn return;rn tryrn stmt=conn.createStatement();rn rs=stmt.executeQuery(strSQL);rnrn if(!rs.next())rn throw new InvalidUserException();rn rn catch(SQLException sqle)rn Debug.log(Debug.getExceptionMsg(sqle));rn rn rn

HTTP Status 500

08-31

package cn.com.gl.blog;rnrnimport java.io.IOException;rnimport java.sql.Connection;rnimport java.sql.PreparedStatement;rnimport java.sql.SQLException;rnrnimport javax.naming.Context;rnimport javax.naming.InitialContext;rnimport javax.servlet.ServletException;rnimport javax.servlet.http.HttpServlet;rnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnimport javax.sql.DataSource;rnrnpublic class BlogServlet extends HttpServlet rn private static final long serialVersionUID = 1L;rn public void doGet(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rn doPost(request, response);rn rnrn public void doPost(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rn request.setCharacterEncoding("UTF-8");rn rn String title = request.getParameter("title");rn String content = request.getParameter("content");rn String categoryId = request.getParameter("category");rn rn DataSource ds = null;rn rn try rn Context context = new InitialContext();rn ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");rn catch (Exception e) rn System.out.println("获取数据源时出错");rn rn rn try rn Connection conn=ds.getConnection();rn String sql = "insert into blog (title,content,category_id,created_time) values (?,?,?,now())";rn PreparedStatement pstmt=conn.prepareStatement(sql);rn pstmt.setString(1, title);rn pstmt.setString(2, content);rn pstmt.setInt(3, Integer.parseInt(categoryId));rn int result=pstmt.executeUpdate();rn System.out.println(result);rn rn catch (SQLException e) rn e.printStackTrace();rn rn rnrnrnrnrnrnrn输入:http://localhost:8080/blog/addBlog.jsprn转到:http://localhost:8080/blog/servlet/BlogServletrnrnrn提示:rntype Exception reportrnrnmessage rnrndescription The server encountered an internal error () that prevented it from fulfilling this request.rnrnexception rnrnjava.lang.NullPointerExceptionrn cn.com.gl.blog.BlogServlet.doPost(BlogServlet.java:41)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:647)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:729)rnrnrnnote The full stack trace of the root cause is available in the Apache Tomcat/5.5.28 logs.rnrnrnrn41行哪里不能为空?谁能帮忙解决一下rnrnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试