我很抱歉再次打扰你的另一个问题但我无法看到原因......我正在看我的代码很长一段时间(我是菜鸟......)。我认为需要查看的文档是displayrecord.jsp,DisplayData servlet,editdata.jsp和EditRecord servlet。如果有人有时间请帮助我,当我点击编辑按钮时,它只会显示一个空白页....
Index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h4>:::::Employees::::</h4>
<ul>
<li><a href="insertrecord.jsp">Create New Employee</a></li>
<li><a href="DisplayData">Display Employees</a></li>
</ul>
</body>
数据库连接Clase:
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class DatabaseConnection {
Connection conn;
Statement stmt;
ResultSet res;
public DatabaseConnection(){
}
public Connection setConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/emp";
conn = DriverManager.getConnection(url,"root","");
}catch(Exception e){ }
return conn;
}
public ResultSet getResult(String sql,Connection conn){
this.conn = conn;
try{
stmt = conn.createStatement();
res = stmt.executeQuery(sql);
}catch(Exception e){ }
return res;
}
}
displayrecord.jsp
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<script type="text/javascript">
function editRecord(id){
url = "EditRecord";
window.location.href = "http://localhost:8084/ZaSkillz/"+url+"?id="+id;
}
</script>
<table align = "center" border="2px" width="80%">
<%Iterator itr;%>
<%List data = (List) request.getAttribute("EmpData");
for(itr = data.iterator(); itr.hasNext();){
%>
<tr>
<% String s = (String) itr.next();%>
<td><%= s %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><input type= "submit" value = "Edit" name="edit" onclick="editRecord(<%=s%>);"></td>
<td><input type= "submit" value = "Delete" name="delete" onclick="deleteRecord(<%=s%>);"></td>
<%}%>
</tr>
</table>
</body>
</html>
DisplayData Servlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DisplayData extends HttpServlet {
String query;
Connection conn;
Statement stmt;
ResultSet res;
DatabaseConnection dbconn;
List lst = new ArrayList();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
dbconn = new DatabaseConnection();
conn = dbconn.setConnection();
stmt = conn.createStatement();
query = "SELECT * from emp";
res = dbconn.getResult(query, conn);
while(res.next()){
lst.add(res.getString("id"));
lst.add(res.getString("ename"));
lst.add(res.getString("desg"));
lst.add(res.getString("sal"));
}
res.close();
}catch(Exception e){
RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
finally {
request.setAttribute("EmpData", lst);
RequestDispatcher rd = request.getRequestDispatcher("/displayrecord.jsp");
rd.forward(request, response);
lst.clear();
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
EditRecord Servlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class EditRecord extends HttpServlet {
Connection conn;
ResultSet res;
Statement stmt;
String id,query;
DatabaseConnection dbconn;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
id= request.getParameter("id");
dbconn = new DatabaseConnection();
conn = dbconn.setConnection();
stmt = conn.createStatement();
query = "SELECT * FROM CONTACT WHERE id= "+ id ;
res = dbconn.getResult(query, conn);
}
catch(Exception e){ out.println(e);}
finally {
request.setAttribute("EditData",res);
RequestDispatcher rd = request.getRequestDispatcher("/editdata.jsp");
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
editdata.jsp:
<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1> WTF</h1>
<form action="EditRecord" method="post">
<table border ="1px" width ="80%">
<%ResultSet res = (ResultSet) request.getAttribute("EditData");%>
<%if(res.next()){
%>
<tr>
<td>ID</td>
<td><input type="text" name="id" value=<%=res.getString("id")%> /></td>
</tr>
<tr>
<td>Ename</td>
<td><input type="text" name="ename" value=<%=res.getString("ename")%> /></td>
</tr>
<tr>
<td>Designation</td>
<td><input type="text" name="desg" value=<%=res.getString("desg")%> /></td>
</tr>
<tr>
<td>Salary</td>
<td><input type="text" name="sal" value=<%=res.getString("sal")%> /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Update" name="update"></td>
</tr>
<%}%>
</table>
</form>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>InsertData</servlet-name>
<servlet-class>InsertData</servlet-class>
</servlet>
<servlet>
<servlet-name>DisplayData</servlet-name>
<servlet-class>DisplayData</servlet-class>
</servlet>
<servlet>
<servlet-name>EditRecord</servlet-name>
<servlet-class>EditRecord</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InsertData</servlet-name>
<url-pattern>/InsertData</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DisplayData</servlet-name>
<url-pattern>/DisplayData</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EditRecord</servlet-name>
<url-pattern>/EditRecord</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>