嗨,实际上我遇到了从jsp页面获取值到java servlet的问题。 在下拉列表中,只有一个下拉值,我能够获得所有其他值为null。不明白我哪里错了。任何人都可以帮助我。
在上传页面中,所有值都很好。但问题仅在于检索页面。我无法检索数据,因为数据没有从html传递到servlet。
这是我的HTML代码:
<form method="post" enctype='multipart/form-data'>
<label>select Board:</label>
<select id="onelevel" name="onelevel" class="changeElem"></select>
<br>
<label>select Syllabus:</label>
<select id="twolevel" name="twolevel" class="showElem changeElem"></select>
<br>
<label>select class:</label>
<select id="threelevel" name="threelevel" class="showElem changeElem"></select>
<br>
<label>select state:</label>
<select id="sixlevel" name="sixlevel" class="showElem changeElem"></select>
<br>
<label>select subjects:</label>
<select id="fourlevel" name="fourlevel" class="showElem changeElem"></select>
<br>
<label>select chapters:</label>
<select id="fivelevel" name="fivelevel" class="showElem changeElem"></select>
<br>
<label>select terms:</label>
<select id="sevenlevel" name="sevenlevel" class="showElem changeElem"></select>
<br>
<button id="retrieve">Submit</button>
</form>
这是我的servlet代码:
package fileretrieve;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import dbConnection.Dbconn;
@MultipartConfig
public class FileRetrieve extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/octet-stream");
PrintWriter out = response.getWriter();
String onelevel=request.getParameter("onelevel");
String twolevel=request.getParameter("twolevel");
String threelevel=request.getParameter("threelevel");
String fourlevel=request.getParameter("fourlevel");
String fivelevel=request.getParameter("fivelevel");
//
//
//
Connection conn = null; // connection to the database
String message = null;
Statement st = null;// message will be sent back to client
try {
// connects to the database
conn = Dbconn.getConnection();
st = conn.createStatement();
PreparedStatement ps=conn.prepareStatement("select data,filename,extension from files1 where board=? AND syllabus=? AND class=? AND subject=? AND chapters=? ");
ps.setString(1,onelevel);
System.out.println("2"+twolevel);
System.out.println("3"+threelevel);
System.out.println("4"+fourlevel);
System.out.println("5"+fivelevel);
ps.setString(2,twolevel);
ps.setString(3,threelevel);
ps.setString(4,fourlevel);
ps.setString(5,fivelevel);
//out.print("<table width=25% border=1>");
// out.print("<center><h1>Result:</h1></center>");
ResultSet rs=ps.executeQuery();
/* Printing column names */
ResultSetMetaData rsmd=rs.getMetaData();
int count=0;
String filenames="\"";
String paths="\"";
while(rs.next())
{
count=count+1;
String extension;
// if(rs.getString(2)=="docx"){
// extension=".docx";
// }else if(rs.getString(2)=="pdf"){
// extension=".pdf";
// }
extension=rs.getString(2).substring(rs.getString(2).lastIndexOf(".") + 1).toLowerCase();
FileOutputStream fos = new FileOutputStream("C:\\Users\\JR00432239\\Desktop\\trial\\WebContent\\filetest"+count+"."+extension);
fos.write(rs.getBytes(1));
fos.close();
// out.print("<tr>");
// out.print("<td>"+rsmd.getColumnName(1)+"</td>");
// out.print("<td><a href=\"file:///C:/Users/JR00432239/Desktop/trial/src/temp/test.pdf\" target=\"_self\">"+rs.getString(2)+"</a></td></tr>");
// getServletContext().getRequestDispatcher("/home.html").forward(request, response);
System.out.println("writing...");
System.out.println(rs.getString(2));
filenames=filenames+rs.getString(2)+";";
paths=paths+"http://localhost:8087/trial/filetest"+count+"."+extension+";";
// filenames=filenames+rs.getString(2)+";";
// paths=paths+"http://localhost:8087/trial/filetest"+extension+";";
//
//
}
filenames=filenames+"\"";
paths=paths+"\"";
response.getWriter().write("{\"name\":"+filenames+",\"path\":"+paths+"}");
// out.print("</table>");
//
}catch (Exception e2)
{
e2.printStackTrace();
}
finally{
// request.setAttribute("data", data);
// RequestDispatcher rd =request.getRequestDispatcher("userview.jsp");
//
out.close();
}
// forwards to the message page
// getServletContext().getRequestDispatcher("/home.html").forward(request, response);
}
}
答案 0 :(得分:0)
我可以看到没有为表单映射的操作。您确定在form / web.xml中映射了正确的操作URL吗?另外,我没有看到select标签的任何选项标签,它们是否动态传递?
答案 1 :(得分:0)
好像你错过了选项标签。每个Select标记应包含至少一个选项标记。任何澄清check this link
答案 2 :(得分:0)
卸下:
enctype='multipart/form-data'
这就是为什么你只能得到第一个参数。将其添加到表单使得servlet处理表单就像它有文件一样,并且你从我能看到的内容中没有任何文件。