我制作了一个程序,用于插入,查看,删除,更新数据库中存储的数据。
但是自从过去3-4个小时后我收到错误: - javax.servlet.ServletException:Servlet执行引发异常
我知道错误是什么,每次我在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>
这是我的代码: -
的index.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>
DataInsertTable.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();
}
}
DataUpdateTable.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());
}
}
}
答案 0 :(得分:0)
在您的web.xml中,您的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>
您编写了DataInsertTable
,但您的班级名为DataInsert
,请修改您的web.xml:
<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>DataInsert</servlet-class>
</servlet>
答案 1 :(得分:0)
我使用你的项目在web.xml中使用这些条目正常工作 同时删除MyServlet条目,因为您没有使用它。 对于编辑模式,你使用/ DataUpdate?mode = EDIT而不是/ DataUpdateTable?mode = EDIT in DataInsertTable.java
<servlet>
<servlet-name>DataInsertTable</servlet-name>
<servlet-class>DataInsertTable</servlet-class>
</servlet>
<servlet>
<servlet-name>DataUpdateTable</servlet-name>
<servlet-class>DataUpdateTable</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataInsertTable</servlet-name>
<url-pattern>/DataInsertTable</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DataUpdateTable</servlet-name>
<url-pattern>/DataUpdateTable</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
希望如果您使用包名称默认
,它将适用于您