我在表中插入一些值并在 servlet 中创建一个新表。我编写了一个代码来捕获 SQLException 并重定向到带有错误消息的 jsp 页面。如果违反了主键约束,则使用 servlet 在表中插入新值时,我的 servlet 没有捕获异常并且我得到一个空白页。
我的小服务程序代码:-
package Servlets;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
/**
*
* @author TALPESH
*/
@MultipartConfig
public class Upload extends HttpServlet {
private final static Logger LOGGER =
Logger.getLogger(Upload.class.getCanonicalName());
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
Connection con;
Statement s;
final String path = "D:/6th sem/MP/OnlineExamination/web/app_data/questionbanks";
final Part filePart = request.getPart("file");
final String fileName = getFileName(filePart);
String branch = request.getParameter("branch");
String sem = request.getParameter("semester");
String course = request.getParameter("course");
String name = request.getParameter("name");
OutputStream out = null;
InputStream filecontent = null;
PrintWriter writer = response.getWriter();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:online_testing");
s=con.createStatement();
String sql="insert into bank(branch,sem,course,bname) values('"+branch+"','"+sem+"','"+course+"','"+name+"')";
s.executeUpdate(sql);
String sql3="CREATE TABLE "+name+"(ID AUTOINCREMENT,question varchar(255),option1 varchar(255),option2 varchar(255),option3 varchar(255),option4 varchar(255),answer varchar(255)) ";
s.executeUpdate(sql3);
s.close();
con.close();
out = new FileOutputStream(new File(path + File.separator
+ fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
request.getSession().setAttribute("msg", "Your Question bank has been uploaded");
response.sendRedirect("fhome.jsp");
}
catch (SQLException ex) {
response.sendRedirect("fhome.jsp");
request.getSession().setAttribute("msg", ex.getMessage().toString());
Logger.getLogger(Upload.class.getName()).log(Level.SEVERE,"exception occurred", ex);
}
catch (ClassNotFoundException ex) {
Logger.getLogger(Upload.class.getName()).log(Level.SEVERE, null, ex);
}
catch (FileNotFoundException fne) {
request.getSession().setAttribute("msg", "You did not specify a file to upload.");
response.sendRedirect("fhome.jsp");
LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}",
new Object[]{fne.getMessage()});
}
finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
if (writer != null) {
writer.close();
}
}
}
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition");
LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
for (String content : part.getHeader("content-disposition").split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(
content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
} // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}