我正在开发一个名为' StudentConnectJSP'的Java Web应用程序。我必须将用户的信息以及从index.jsp中提取的上传照片存储到数据库“t'使用servlet' CreateAccount.java'。我有一个servlet' ImageFetch'从数据库中获取图像。现在,当我尝试显示来自' ImageFetch'到home.jsp,它不起作用。这是代码。
的index.html
<%@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>Hello World!</h1>
<H1>CREATE ACCOUNT</H1>
<br>
<form action="CreateAccount" method="post"
enctype = "multipart/form- data">
NAME
<br>
<input class="in" type="text" placeholder="FIRST NAME" name="fname">
<input class="in" type="text" placeholder="LAST NAME" name="lname">
<br>
ROLL NO.<br>
<input class="in" type="text" name="roll">
<br>
UNIVERSITY
<input type="text" name="univ"><br>
COLLEGE<br>
<input class="in" type="text" name="college">
<BR>
PHOTO<input type="file" name="photo">
<br>
GENDER <br>
<input type="radio" name="sex" value="male">MALE
<input type="radio" name="sex" value="female">FEMALE
<br>
create password<br>
<input class="in" type="TEXT" placeholder="PASWORD" name="pass">
<input id="sub" type="submit" value="signin">
</form>
</body>
</html>
CreateAccount.java
package com.studentconnect;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215)
public class CreateAccount extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws
IOException, ServletException{
res.setContentType("text/html");
int flag = 0;
String n;
String fname = req.getParameter("fname");
String lname = req.getParameter("lname");
int rno = Integer.parseInt(req.getParameter("roll"));
String pass = req.getParameter("pass");
String univ = req.getParameter("univ");
String college = req.getParameter("college");
String sex = (req.getParameter("sex"));
InputStream is = null;
Part filePart = req.getPart("photo");
if (filePart != null) {
is = filePart.getInputStream();
}
PrintWriter out = res.getWriter();
out.print("<html><body>");
out.print("<b>Servlet Started</b>");
out.print("<p>" + is + "</p>");
out.print("<p>" + rno + "</p>");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root",
"root");
if(!con.isClosed()){
System.out.println("Connected to MySQL");
Statement stmt=con.createStatement();
stmt.executeUpdate("create table if not exists t(id int(10) NOT
NULL AUTO_INCREMENT, fname varchar(10), lname varchar(10), rno int(10),
photo mediumblob, univ varchar(25), college varchar(25), sex varchar(5),
pass varchar(20), PRIMARY KEY(id))");
String sql = "insert into t (fname, lname, rno, photo, univ,
college, sex, pass) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, fname);
pst.setString(2, lname);
pst.setInt(3, rno);
if(is != null){
pst.setBlob(4, is);
}
pst.setString(5, univ);
pst.setString(6, college);
pst.setString(7, sex);
pst.setString(8, pass);
int i = pst.executeUpdate();
if(i > 0){
HttpSession session = req.getSession(true);
session.setAttribute("user", fname);
session.setMaxInactiveInterval(30);
//RequestDispatcher rd =
req.getRequestDispatcher("ImageFetch");
//rd.forward(req,res);
res.sendRedirect("home.jsp");
}
else{
RequestDispatcher rd =
req.getRequestDispatcher("index.jsp");
out.println("<font color=red>Either user name or password is wrong.
</font>");
rd.include(req, res);
}
out.print("<p>Account successfully created.</p>");
}
con.close();
}catch(ClassNotFoundException c){
System.out.println(c);
}
catch(Exception e){
System.out.println(e);
}
out.print("</body></html>");
}
}
针对home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<%
if (session != null) {
if (session.getAttribute("user") != null) {
String name = (String) session.getAttribute("user");
out.print("Hello, " + name + " Welcome to ur Profile");
out.print("<hr>");
} else {
response.sendRedirect("index.jsp");
}
}
%>
</br>
<img src="/StudentConnectJSP/ImageFetch"/>
</br>
<form action="ImageFetch" method="post">
<input type="submit" value="Show">
</form>
</body>
</html>
ImageFetch.java
package com.studentconnect;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ImageFetch extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
String connectionURL = "jdbc:mysql://localhost:3306/student";
java.sql.Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(connectionURL,"root","root");
Statement st1=con.createStatement();
ResultSet rs1 = st1.executeQuery("select photo from"+
" t where id='2'");
String imgLen="";
if(rs1.next()){
imgLen = rs1.getString(1);
System.out.println(imgLen.length());
}
rs1 = st1.executeQuery
("select photo from t where id='2'");
if(rs1.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
System.out.println("index"+index);
st1.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
RequestDispatcher RequetsDispatcherObj
=request.getRequestDispatcher("/home.jsp");
RequetsDispatcherObj.forward(request, response);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}