在JSP

时间:2017-12-18 12:53:20

标签: java jsp

我想将表单页面中的图像和其他文本字段添加到数据库中。我没有收到错误但是当我点击按钮时我得到空白页面。请解决我的问题..在add_ckt_products.jsp文件中我的表单元素可用,在uploadFile.jsp文件中我的数据库连接和查询可用。

ckt_products.jsp

<form class="form-horizontal" enctype="multipart/form-data" action="uploadFile.jsp">
                          <fieldset>
<div class="control-group"> 

                                                            <label class="control-label" for="typeahead">Product Image</label>
                                                            <div class="controls">
                                                                <input type="file" class="form-control" name="ck_product_image" id="txtimage" required="required">
                                                                <span>Image Size (698 X 352)</span>
                                                            </div>
                                                         </div>


                            <div class="control-group">
                              <label class="control-label" for="typeahead">Product Name: </label>
                              <div class="controls">
                                <input type="text" class="span6 typeahead" id="typeahead" name="ck_product_name" data-provide="typeahead" data-items="4"   reqired=""/>
                              </div>
                            </div>

                                                         <div class="control-group">
                              <label class="control-label" for="typeahead">Product Type: </label>
                              <div class="controls" >
                                                              <input type="radio" class="span6 typeahead" id="typeahead" name="ck_product_type" data-provide="typeahead" data-items="4" value="Cricket Bats" required=""/>Cricket Bats
                                                              <input type="radio" class="span6 typeahead" id="typeahead" name="ck_product_type" data-provide="typeahead" data-items="4" value="Tennis Cricket Ball" required=""/>Tennis Cricket Ball
                              </div>
                            </div>

                                                        <div class="control-group">
                              <label class="control-label" for="typeahead">Product Brand: </label>
                              <div class="controls">
                                                              <input type="checkbox" class="span6 typeahead" id="typeahead" name="ck_product_brand" data-provide="typeahead" value="MRF" data-items="4"/>MRF
                                                              <input type="checkbox" class="span6 typeahead" id="typeahead" name="ck_product_brand" data-provide="typeahead" value="TON" data-items="4"/>TON
                                                              <input type="checkbox" class="span6 typeahead" id="typeahead" name="ck_product_brand" data-provide="typeahead" value="SG" data-items="4"/>SG
                                                              <input type="checkbox" class="span6 typeahead" id="typeahead" name="ck_product_brand" data-provide="typeahead" value="PUMA" data-items="4"/>PUMA
                                                              <input type="checkbox" class="span6 typeahead" id="typeahead" name="ck_product_brand" data-provide="typeahead" value="NIVIA" data-items="4"/>NIVIA
                                                              <input type="checkbox" class="span6 typeahead" id="typeahead" name="ck_product_brand" data-provide="typeahead" value="GM" data-items="4"/>GM
                                                              <input type="checkbox" class="span6 typeahead" id="typeahead" name="ck_product_brand" data-provide="typeahead" value="GURU" data-items="4"/>GURU
                              </div>
                            </div>
                            <div class="control-group">
                              <label class="control-label" for="typeahead">Product Price: </label>
                              <div class="controls">
                                <input type="number" class="span6 typeahead" id="typeahead" name="ck_product_price" data-provide="typeahead" data-items="4"   reqired="true"/>
                              </div>
                            </div>
                            <div class="control-group">
                              <label class="control-label" for="typeahead">Product Price Range: </label>
                              <div class="controls">
                                                              <input type="radio" class="span6 typeahead" id="typeahead" name="ck_product_price_range" data-provide="typeahead" data-items="4"   value="Under 500" required=""/>Under &#8377;500
                                                              <input type="radio" class="span6 typeahead" id="typeahead" name="ck_product_price_range" data-provide="typeahead" data-items="4"   value="Under 1000" required=""/>Under &#8377;1000
                                                              <input type="radio" class="span6 typeahead" id="typeahead" name="ck_product_price_range" data-provide="typeahead" data-items="4"   value="Between 1000 1500" required=""/>Between &#8377;1000 to &#8377;1500
                              </div>
                            </div>
                            <div class="form-actions">
                              <button type="submit" class="btn btn-primary">Save changes</button>
                              <button type="reset" class="btn">Cancel</button>
                            </div>

                          </fieldset>
                        </form>   

uploadFile.jsp

<%-- 
    Document   : uploadFile
    Created on : Jul 17, 2017, 10:33:29 AM
    Author     : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%
    int no1=1; 
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0))
{

DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
out.println("sdf1");
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("E:/Parth_Enterprise/web/img/gallery/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/parth_enterprise?zeroDateTimeBehavior=convertToNull";
PreparedStatement psmnt = null;
try{
    String ck_product_type=request.getParameter("ck_product_type");
                    String ck_product_name=request.getParameter("ck_product_name");
                    String ck_product_brand[]=request.getParameterValues("ck_product_brand");
                        String brand="";
                         for(String b:ck_product_brand)
                         {
                             brand=brand+b;
                         }
                    int ck_product_price=Integer.parseInt(request.getParameter("ck_product_price"));
                    String ck_product_price_range=request.getParameter("ck_product_price_range");

Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root","");
psmnt = connection.prepareStatement("insert into  cricket_product(ck_product_id,ck_product_image,ck_product_type,ck_product_name,ck_product_brand,ck_product_price,ck_product_price_range,ck_product_status) values(?,?,?,?,?,?,?,?)");
psmnt.setString(1,null);
psmnt.setString(2,ff.getPath());
psmnt.setString(3,ck_product_type);
psmnt.setString(4,ck_product_name);
psmnt.setString(5,brand);
psmnt.setInt(6,ck_product_price);
psmnt.setString(7,ck_product_price_range);
psmnt.setString(8,"In Stock");
int s = psmnt.executeUpdate();
if(s>0)
{
    response.sendRedirect("http://localhost:8084/Parth_Enterprise/index.jsp?mo=view_ckt_products");

}
else
{
System.out.println("Error!");
}
}
catch(Exception e)
{
    out.println(e);
           e.printStackTrace();
}
}
%>

0 个答案:

没有答案