Ошибка при разборе в JSP

Я пытаюсь сохранить некоторые значения в базе данных MySql. Значение взято из страницы jsp. Но, похоже, есть некоторые проблемы с синтаксическим разбором, которые я не могу понять. Здесь мой код

JSP

<%@page import="register.register"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>



<title>***********(India)</title>






 <%//**The Problem is Somewhere in this part of code**
 String stdname = request.getParameter("student");
 String batch = request.getParameter("batch");
 String email = request.getParameter("email");
 String contact = request.getParameter("contact");
 String msg;
 String fees=request.getParameter("fees");
 int stdcontact=Integer.parseInt(contact);
 int stdfees=Integer.parseInt(fees);
 if (stdname!= null ) {
 register objadd = new register();
 boolean flag = objadd.InsertNew(stdname,batch,email,stdcontact,stdfees);
 if (flag) {
 request.getRequestDispatcher("success.jsp").forward(request, response);
 } else {
 msg = "Invalid data entered!";
 }
 }
 %>
<form action="Register.jsp" method="POST">

<div id="header">
 <div id="logo">
 
 <h3><a href="#" target="_blank">***********</a></h3>
 </div>
 <div id="menu">
 <ul>
 <li><a href="#" title="" target="_blank">New Batch</a></li>
 <li><a href="Login.jsp" title="" target="_blank">Register Student</a></li>
 <li><a href="AddEmployeeData.jsp" title="" target="_blank">Fees Record</a></li>
 <li><a href="AddKeywords.jsp" title="" target="_blank">Manage Student</a></li>
 <li><a href="StopWordDetectionForm.jsp" title="" target="_blank">All Details</a></li>
 </ul>
 </div>
 
</div>
<div id="page">
 <div id="content">
 <div>
 <h2>Welcome to SPAM Management</h2>
 <div>
 
 
 



<blockquote>


<p><font size="3">Student Name 
</font>
</p>
<p><font size="3">Batch<select name="batch">
 <option>Java</option>
 <option>PHP</option>
 <option>Framework</option>
 <option>Android</option>
 </select>
</font>
</p>
<p><font size="3">Email ID 
</font>
</p>
<p><font size="3">Contact No. 
</font>
</p>
 <p><font size="3">Fees 
</font>
</p> 



 </blockquote></div>
 </div>
 </div>
 <!-- end #content -->
 <div id="sidebar">
 <div>
 <h2>Key Features: </h2>
 <div>
 <ul>
 <li>***********(India) is one of the major leading training institute in Software Development in India.</li>
 <li>Training provided in Java,PHP,Android,database,etc</li>
 <li>Live project development is also provided</li>
 </ul>
 </div>
 </div>
 </div>
 <!-- end #sidebar -->
</div></form>
<!-- end #page -->
<div> </div>
<div id="footer">
 <p id="legal">Copyright © ***********(India) All Rights Reserved.</p>
 <p id="links"><a href="#" target="_blank">Privacy Policy</a> | <a href="#" target="_blank">Terms of Use</a></p>*/
</div>

Класс Java

package register;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author asus
 */
public class register {


 public boolean InsertNew(String stdname,String stdbatch,String email,int contact,int fees){
 boolean flag = false;
 Connection con = null;
 String connectionURL = "jdbc:mysql://localhost:3306/***********";

 Connection connection = null;
 ResultSet rs; 

 try {
 // Load the database driver
 Class.forName("com.mysql.jdbc.Driver");
 // Get a Connection to the database
 connection = DriverManager.getConnection(connectionURL, "root", "");
 //Add the data into the database

 //String sql = "insert into sample values(?,?)";

 PreparedStatement pst = connection.prepareStatement("INSERT into alldetails VALUES(?,?,?,?,?,?)");

 pst.setString(1,null);
 pst.setString(2,stdname);
 pst.setString(3,email);
 pst.setInt(4,contact);
 pst.setString(5,stdbatch);
 pst.setInt(6,fees);


 int numRowsChanged = pst.executeUpdate();
 if(numRowsChanged!=0){
 System.err.println("Successfull execution");
 }
 else{
 System.err.println("Problem with the insertion query");
 }
 pst.close();
 }
 catch(ClassNotFoundException e){
 e.printStackTrace();
 }
 catch(SQLException e){
 e.printStackTrace();
 }
 catch (Exception e){
 e.printStackTrace();
 }
 finally {
 // Always close the database connection.
 try {
 if (connection != null) connection.close();
 }
 catch (SQLException ignored){
 }
 }

 return true;
 }


}

ошибка

HTTP Status 500 - An exception occurred processing JSP page /Register.jsp at line 21

type Exception report

message An exception occurred processing JSP page /Register.jsp at line 21

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /Register.jsp at line 21

18: String contact = request.getParameter("contact");
19: String msg;
20: String fees=request.getParameter("fees");
21: int stdcontact=Integer.parseInt(contact);
22: int stdfees=Integer.parseInt(fees);
23: if (stdname!= null ) {
24: register objadd = new register();


Stacktrace:
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

java.lang.NumberFormatException: null
 java.lang.Integer.parseInt(Integer.java:454)
 java.lang.Integer.parseInt(Integer.java:527)
 org.apache.jsp.Register_jsp._jspService(Register_jsp.java:84)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.

Проблема, кажется, в файле JSP... Пожалуйста, помогите мне разобраться в этом

1 ответ

Стекло ясно указывает исключение

NumberFormatException

на линии

int stdcontact=Integer.parseInt(contact);

где контакт равен null

parseInt()

Броски:

NumberFormatException - если строка не содержит целочисленное целое число.

в вашем случае это значение null, которое не поддается анализу.

Окружайте свой код с помощью try/catch а также добавьте null проверку.

licensed under cc by-sa 3.0 with attribution.