我制作了一个用于插入、查看、删除、更新数据库中存储数据的程序。
但是自从过去 3-4 小时以来,我收到了错误:- javax.servlet.ServletException: Servlet execution throw an exception
每次我在 web.xml 文件中犯错误时,我都知道错误是什么。因为我总是对配置 web.xml 文件感到困惑。所以我得到“资源不可用错误”任何人都可以帮助我。我已经发布了 web.xml 文件有人可以帮我解决这个问题吗?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DataInsert</servlet-name>
<servlet-class>DataInsertTable</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DataUpdate</servlet-name>
<servlet-class>DataUpdateTable</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DataInsert</servlet-name>
<url-pattern>/DataInsert</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DataUpdate</servlet-name>
<url-pattern>/DataUpdate</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这是我的代码:-
索引.jsp
<html>
<head>
<title>Data insertion form</title>
</head>
<body>
<%
String action="/SimpleServlet/DataInsertTable";
String method="get";
if(request.getAttribute("EDIT")!=null)
{
action="/SimpleServlet/DataUpdateTable";
method="post";
}
%>
<form method= "<%=method%>" action="<%=action%>">
<h1><center>Enter the required information</center></h1>
<table>
<tr>
<td>Name</td>
<td> :- </td>
<%if(request.getAttribute("NAME")!=null){ %>
<td><input type="text" name="name" value="<%=request.getAttribute("NAME")%>"></input>
<%}else{ %>
<td><input type="text" name="name"></input>
<%} %>
</td>
</tr>
<tr>
<td>Roll Number</td>
<td> :- </td>
<%if(request.getAttribute("ROLL")!=null){ %>
<td><input type="text" name="roll" value="<%=request.getAttribute("ROLL")%>"></input>
<%}else{ %>
<td><input type="text" name="roll"></input>
<%} %>
</td>
</tr>
<tr>
<td>Class </td>
<td> :- </td>
<%if(request.getAttribute("CLAS")!=null){ %>
<td><input type="text" name="clas" value="<%=request.getAttribute("CLAS")%>"></input>
<%}else{ %>
<td><input type="text" name="clas"></input>
<%} %>
</td>
</tr>
<tr>
<td>Mobile Number </td>
<td> :- </td>
<%if(request.getAttribute("MONO")!=null){ %>
<td><input type="text" name="mono" value="<%=request.getAttribute("MONO")%>"></input>
<%}else{ %>
<td><input type="text" name="mono"></input>
<%} %>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" width="16"></input></td>
</tr>
</table>
</form>
</body>
</html>
数据插入表.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataInsert extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PrintWriter pwinsert = res.getWriter();
// PrintWriter pwdelete = res.getWriter();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st = null;
out.println("<html>");
out.println("<head>");
out.println("<title>User Data</title>");
out.println("</head>");
out.println("<body>");
out.println("<center><u><h1>User Data</h1></u>");
out.println("<form name='form' >");
int a = 2;
out.println("<table border="+a+ "> ");
out.println("<tr>");
out.println("<td> Select </td>");
out.println("<td> Name </td>");
out.println("<td> Roll No. </td>");
out.println("<td> Class </td>");
out.println("<td> Mobile Number </td>");
out.println("<td> Edit </td>");
out.println("</tr>");
String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("driver not loaded");
System.exit(0);
}
String URL = "jdbc:oracle:thin:@192.168.106.87:1521:ORA11G";
String Username = "pratik";
String Password = "pratik";
try {
con = DriverManager.getConnection(URL,Username,Password);
if(req.getParameter("choise")==null) {
ps = con.prepareStatement("INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ? )");
ps.setString(1,nm);
ps.setString(2,roll);
ps.setString(3,clas);
ps.setString(4,mono);
int i = ps.executeUpdate();
pwinsert.println(i);
if(i!=0) {
pwinsert.println("Your data has been stored in the database");
}
else {
pwinsert.println("Your data could not be stored in the database");
}
}
}
catch(Exception e) {
pwinsert.println(e.getMessage());
}
String idr=null;
if(req.getParameter("choise")!=null) {
nm = req.getParameter("choise");
idr = "DELETE from student WHERE name ='"+nm+"'";
try {
st=con.createStatement();
rs = st.executeQuery(idr);
}
catch (Exception e) {
e.getMessage();
System.out.println("Error " +e);
}
System.out.println("Data deleted...");
}
// res.sendRedirect("DataInsertTable.java");
idr = "SELECT * FROM student";
try {
st=con.createStatement();
rs = st.executeQuery(idr);
}
catch (Exception e) {
e.getMessage();
System.out.println("Error " +e);
}
try {
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + "<input type=\"radio\" name=\"choise\" value=\"" + rs.getString(1) + "\" /> </br>" + "</td>");
out.println("<td>" + rs.getString(1) + "</td>" + "\t <td>" + rs.getInt(2) + "</td>" + "\t <td>" + rs.getString(3) + "</td>" + "\t <td>" + rs.getString(4));
out.println("<td>" + "<a href='"+req.getContextPath()+"/DataUpdate?mode=EDIT&name="+rs.getString(1)+"&roll="+rs.getInt(2)+"&clas="+rs.getString(3) +"&mono="+rs.getString(4)+"'>Edit</a> </br>" + "</td>");
out.println("</tr>");
}
out.println("<tr>");
out.println("<td> </td>");
out.println("<td> </td>");
out.println("<td>" + "<input type=\"submit\" value=\"Delete\" />" + "</td>");
out.println("<td> </td>");
out.println("<td> </td>");
out.println("</tr>");
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
try {
rs.close();
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
try {
st.close();
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
out.println("</table>");
out.println("</form>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
数据更新表.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataUpdateTable extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PrintWriter pwinsert = res.getWriter();
// PrintWriter pwdelete = res.getWriter();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st = null;
out.println("<html>");
out.println("<head>");
out.println("<title>User Data</title>");
out.println("</head>");
out.println("<body>");
out.println("<center><u><h1>User Data</h1></u>");
out.println("<form name='form' >");
int a = 2;
out.println("<table border="+a+ "> ");
out.println("<tr>");
out.println("<td> Name </td>");
out.println("<td> Roll No. </td>");
out.println("<td> Class </td>");
out.println("<td> Mobile Number </td>");
out.println("</tr>");
String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");
req.setAttribute("NAME", nm);
req.setAttribute("ROLL", roll);
req.setAttribute("CLAS", clas);
req.setAttribute("MONO", mono);
req.setAttribute("EDIT", "Y");
req.getRequestDispatcher("/index.jsp").forward(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PrintWriter pwinsert = res.getWriter();
// PrintWriter pwdelete = res.getWriter();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("driver not loaded");
System.exit(0);
}
String URL = "jdbc:oracle:thin:@192.168.106.87:1521:ORA11G";
String Username = "pratik";
String Password = "pratik";
try {
con = DriverManager.getConnection(URL,Username,Password);
String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");
if(req.getParameter("choise")==null) {
ps = con.prepareStatement("update student set name=?, rollno=?, class=?, mobileno=? where rollno=?");
ps.setString(1,nm);
ps.setString(2,roll);
ps.setString(3,clas);
ps.setString(4,mono);
ps.setString(5,roll);
int i = ps.executeUpdate();
pwinsert.println(i);
if(i!=0) {
pwinsert.println("Your data has been stored in the database");
}
else {
pwinsert.println("Your data could not be stored in the database");
}
}
}
catch(Exception e) {
pwinsert.println(e.getMessage());
}
}
}