Only File is uploading to database, all other fields are NULL - javascript

So as the title says, I'm inserting information in mysql database but only the file that I'm uploading is inserted into database. Everything else is NULL. I suspect it has something to do with the creation of temporary directory on the server to save the file but then again I can't find the solution.
Here is the table I'm inserting into
CREATE TABLE DOC(IDD INT NOT NULL AUTO_INCREMENT, DOCN VARCHAR(50), AUTHOR VARCHAR(50), CAT VARCHAR(50),CONTENT MEDIUMBLOB NOT NULL, CRITN INT DEFAULT 0, PRIMARY KEY(IDD));
Here is the html form I'm fiiling
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form enctype="multipart/form-data" action="uploadfile.jsp" method="post" onsubmit="return verify()">
<table border='1'>
<tr>
<span style="color:black">Τίτλος</span> <input type="text" name="title"><p></p>
<span style="color:black">Συγγραφέας</span> <input type="text" name="author"><p></p>
<span style="color:black">Κατηγορία</span> <input type="text" name="cat"><p></p>
<td>
Επιλέξτε το άρθρο που θέλετε.
</td>
</tr>
<tr>
<td>
<input type="file" name="filename" id="filename"accept="application/pdf"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Upload" />
</td>
</tr>
</table>
</form>
</body>
</html>
And here's the code for the jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.io.InputStream"%>
<%#page import="conPackage.MyConnection"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.io.FileInputStream"%>
<%#page import="java.util.Enumeration"%>
<%#page import="com.oreilly.servlet.MultipartRequest"%>
<%#page import="java.io.File"%>
<%# page import="java.util.Properties.*" %>
<%# page import="java.security.MessageDigest;"%>
<%# page import="java.util.*"%>
<%#page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String rtempfile = File.createTempFile("temp","1").getParent();
MultipartRequest multi = new MultipartRequest(request,rtempfile, 15*1024*1024);
Enumeration files = multi.getFileNames();
String docn =request.getParameter("title");
String author = request.getParameter("author");
String cat= request.getParameter("cat");
String st="insert into doc(docn, author, cat, content) values (?,?,?,?)";
PreparedStatement psmt=MyConnection.getConnection().prepareStatement(st);
String name="";
String fileExtesion="";
File ff =null;
FileInputStream fin =null;
while (files.hasMoreElements())
{
name=(String)files.nextElement();
ff = multi.getFile(name);
fileExtesion = ff.getName().substring(ff.getName().lastIndexOf("."));
// check user has select the correct file or not
boolean fileAllowed = fileExtesion.equalsIgnoreCase(".pdf");
if((ff!=null)&&fileAllowed)
{
try
{
fin=new FileInputStream(ff);
psmt.setString(1, docn);
psmt.setString(2, author);
psmt.setString(3, cat);
psmt.setBinaryStream(4,(InputStream)fin, (int)(ff.length()));
boolean sss = psmt.execute();
out.print("uploaded successfully..");
out.print("<br/> Go to <a href='downloadfile.jsp'>Download</a> page");
}
catch(Exception e)
{
out.print("Failed due to " + e);
}
finally
{
fin.close();
ff.delete();
}
}
else
{
out.print("Please select the correct file...");
}// end of if and else
}// end of while
MyConnection.CloseConnection(); // close the connection
%>
</body>
</html>

When using enctype multipart/form-data you have to use request.getPart for the regular parameters too, as request.getParameter always returns null with multipart/form-data. The other option for processing file uploads is the Apache Commons File Upload library, but in that case as well, you have to get the regular parameters wit the same method as you get the file.

Related

jstl not executeing within jsp file loaded using jquery load()

i have partial jsp file with jstl code :
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<form method="POST" action="EntrerClient" id="submitparticulier"
class=submitparticulier">
<fieldset>
<input type="hidden" id="Id" name="idform" value="inscp">
<select name="genre" class="genre">
<option>Monsieur</option>
<option>Melle/Madame</option>
</select>
</br>
<label for="etat">Nom <span class="requis">*</span></label></br>
<input type="text" id="nom" name="nom" value="<c:out
value="${particulier.getNom()}"/>" size="20" maxlength="60" />
<span class="erreur">${form.erreurs['nom']}</span>
<br />
<p class="${empty form.erreurs ? 'succes' :
'erreur'}">${form.resultat}</p>
<br />
<p id="adcli"><a href='#ADCLI'>Valider</a></p>
</fieldset>
</form>
and principal jsp :
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1
/jquery.min.js"></script>
<script src="ressources/_javasctipt.js"></script>
<script type="text/javascript">
constructform("<c:out value="${param['par']}"/>");
</script>
...
the partial file is loaded using jquery :
...
function constructform(param) {
var principal = $("#Gc_FenettrePrincipale");
principal.empty();
principal.append("<div class=\"Gc_FenettreSec\" id=\"_ADD\"><p>Ajout
Clients</p></div>");
principal.append("<div style=\"width:700px;padding:20px;S\">");
principal.append("<div id=\"ajout_client\" class=\"ajout_client\">");
principal.append("</div></div>");
var add=$("#add");
add.removeClass('current');
$("#adcli").addClass('current');
$('#ajout_client.div').remove();
var form_particulier=$("<div/>");
form_particulier.load("ressources/PaletParticulier.jsp");
//form_particulier.html();
var formul = $("#ajout_client");
formul.append("<select name=\"type\" class=\"type
\"><option>Particulier</option><option>Organisme</option></select>");
formul.append(form_particulier);
} ...
the
<input type="text" id="nom" name="nom" value="<c:out
value="${particulier.getNom()}"/>"
servlet call
...
request.setAttribute(ATT_FORM,form);
request.setAttribute(ATT_PARTICULIER,particulier);
response.setContentType("text/html");
this.getServletContext().getRequestDispatcher(VUE).forward(request,
response);
in the partial jsp does not work but it work in the principal jsp if used,i can't figure why
in the java channel in the irc thy suggeced to go to jsf,i can't to that now,am off of deley
Idea 1: If your JavaScript function constructform is in /ressources/_javasctipt.js, then the load request might look for /ressources/ressources/partial.jsp due to the relative URL.
Idea 2: Have you tried omitting the <!DOCTYPE html> in your partial JSP?
i figured out that load() function is http resquet,GET or POST ,and am using servlet to load jsp,so the soution is to create the hol jsp.
i don't see better solution
"an other servlet or an other jsp"

Not getting value in <c:set var from scriptlet variable

I am defining a variable in scriptlet to get value from request headers, then we are using that variable c:set to set to variable to use in c:out .But i am not getting any value c:set variable, as a result c:out is giving result as ''.
please find the code snippet below and guide me if i am missing anything.
Code in jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>DM</title>
<SCRIPT Language="JavaScript">
function submitform()
{
this.LoginForm.submit();
}
</SCRIPT>
</head>
<body onload="submitform()">
<%
String userName= request.getHeader("IV-USER");
System.out.println("name "+userName);
if (userName == null || userName.length()==0){
response.setHeader("IV-USER",userName);
response.sendRedirect("ULM.jsp");
}
%>
<c:set var="uName" value="<%=userName%>"/>
<p>Welcome1 ${uName}</p>
<form name="LoginForm" action="/ICDDMContent/STGDM.html" method="post">
<input type="hidden" name="UserId" value='<c:out value="${uName}"/>'/>
<!--
<input type="hidden" name="UserId" value="<%=userName%>" >
-->
<input type="hidden" name="Token" value="dummy">
<p>Welcome1 <c:out value="${uName}"/></p>
</form>
</body>
</html>
String userName= request.getHeader("IV-USER");
System.out.println("name "+userName);
if (userName == null || userName.length()==0){
response.setHeader("IV-USER",userName);
response.sendRedirect("ULM.jsp");
}
Is redundant piece of code, if userName is empty, you will just assign empty value to IV-USER header, if "ULM.jsp" is the same page as you've attached, and that redirect is just to set variable - here is your problem

Can't refresh page and hold selected value on jsp page

Here is my jsp page code:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# page import="java.sql.*"%>
<%# page import="java.io.*"%>
<%# page import="java.util.*"%>
<%# page import="test.Obiekt"%>
<%# page import="test.ListaObiektow"%>
<%# page import="test.Termin"%>
<%# page import="test.ListaTerminow"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>menu główne</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<meta name="android-mobile-web-app-capable" content="yes">
<meta name="android-mobile-web-app-status-bar-style" content="black">
<link href="css/ratchet.css" rel="stylesheet">
<link href="css/ratchet-theme-android.css" rel="stylesheet">
<script src="js/ratchet.js"></script>
<script type="text/javascript">
function Refresh(idObiekt){
location.href="pilkaNozna.jsp?idObiekt=" + idObiekt;
}
</script>
</head>
<body>
</br>
</br>
</br>
<header class="bar bar-nav">
<a class="icon icon-left-nav pull-left" href="wyszukaj.jsp"></a>
<h1 class="title">Wybierz obiekt</h1>
</header>
<div id="content">
<div class="tabelawybor">
<b>Wybierz obiekt:</b>
<%
ArrayList<Obiekt> list = new ListaObiektow().getObiekty();
%>
<form name="obiekt">
<select name="obiekt" onChange="Refresh(this.value)">
<%
for (Obiekt obiekt : list) {
String selectedObiekt = request.getParameter("obiekt");
%>
<option value="<%=obiekt.idObiekt%>"
<%= ((Integer.toString(obiekt.idObiekt)).equals(selectedObiekt))?"selected":""%>><%=obiekt.nazwa%>
<%=obiekt.adres%></option>
<%
int idObiekt = obiekt.idObiekt;
request.setAttribute("idObiekt", idObiekt);
%>
<%
}
%>
</select>
</form>
</div>
<div class="tabelawybor">
<td><b>Wpisz liczbę uczestników:</b><input type="text"
name="uczest" /></td>
</div>
<div class="tabelawybor">
<table class="center">
<tr>
<td>Nazwa obiektu:</td>
<td>Data:</td>
<td>Godzina</br> rozpoczęcia:
</td>
<td>Godzina</br> zakończenia:
</td>
<td></td>
</tr>
<%
ArrayList<Termin> lista = new ListaTerminow().getTerminy();
for (Termin termin : lista) {
%>
<tr>
<td><%=termin.nazwaObiektu%> <%=termin.adresObiektu%></td>
<td><%=termin.dzien%></td>
<td><%=termin.odKtorej%></td>
<td><%=termin.doKtorej%></td>
<td><form action="Rezerwuj" method="post">
<button class="btn btn-primary">Zarezerwuj</button>
</form></td>
</tr>
<%
}
%>
</table>
</div>
</div>
</body>
</html>
ListaTerminow.java
package test;
import test.ConnectionClass;
import test.ListaObiektow;
import test.Obiekt;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import test.ListaObiektow;
public class ListaTerminow {
Connection conn;
int idObiekt;
public ListaTerminow() throws SQLException
{
conn = ConnectionClass.Polacz();
}
public ArrayList<Termin> getTerminy() throws SQLException, ClassNotFoundException
{
ArrayList<Termin> terminy = new ArrayList<Termin>();
ResultSet rs = null;
String query = "SELECT obiekty.nazwa,obiekty.adres, termin.dzien, termin.odKtorej, termin.doKtorej FROM termin LEFT JOIN obiekty ON termin.idObiekt = obiekty.idObiekt WHERE termin.czyZajety = false AND obiekty.idObiekt = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setLong(1, idObiekt);
rs = ps.executeQuery();
while(rs.next())
{
Termin termin = new Termin();
termin.setNazwaObiektu(rs.getString(1));
termin.setAdresObiektu(rs.getString(2));
termin.setDzien(rs.getDate(3));
termin.setOdKtorej(rs.getString(4));
termin.setDoKtorej(rs.getString(5));
terminy.add(termin);
}
return terminy;
}
}
I found solution to refresh and remember value after select option from select form. But now, i want to show all "termins" in below select form. For example: When i select first option in select form (obiekt.idObiekt = 1) then i want to show all termins for obiekt with id 1 in . Can you help me ?
First of all, put your select inside a form. So that when you submit it during refresh you can get the value it was holding with String selectedObiekt=request.getParameter("obiekt"). Then modify your option to read
<option value="<%=obiekt.idObiekt%>" <%= ((Integer.toString(obiekt.idObiekt)).equals(selectedObiekt))?"selected":""%>><%=obiekt.nazwa%>
<%=obiekt.adres%></option>

how to use javascript function for fetching data at 15 minute interval?

I have Date_time field with yyyy-mm-dd hh:mm:ss format in my database. I had stored 8 days data in my database. Now i want data at every 15 minute. what is the solution for it? please help me.
now i want this value at every 15 minute interval. What can i do??? I have no more idea about javascript n jquery.
my code is:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Fetching data from the database</title>
</head>
<body>
<table border="2">
<tr>
<th>Inv_id</th>
<th>Inv_phase1_V</th>
<th>Inv_phase1_A</th>
<th>Inv_phase1_kW</th>
</tr>
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/solar";
String username="root";
String password="root";
Connection conn=DriverManager.getConnection(url,username,password);
String query="select Inv_id,Inv_phase1_V,Inv_phase1_A,Inv_phase1_kW from inverter_detail";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
String Inverter_id = rs.getString("Inv_id");
Double voltage = rs.getDouble("Inv_phase1_V");
Double ampere = rs.getDouble("Inv_phase1_A");
Double kiloWatt = rs.getDouble("Inv_phase1_kW");
%>
<tr>
<td><%=Inverter_id%></td>
<td><%=voltage%></td>
<td><%=ampere%></td>
<td><%=kiloWatt%></td>
</tr>
<%
}
%>
<%
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</table>
</body>
</html>

autocomplete list in input text box is not coming

So i am having problem on auto completion feature of jquery. My requirement is to show Json data in auto completion . Json data is coming from java class in jsp page .This code is working fine but nothing is coming in text box as i type something.
This is my jsp page
<%#page import="com.practise.autoComplete.AutoComplete"%>
<%#page import="net.sf.json.JSONObject"%>
<%#page import="net.sf.json.JSON"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="java.util.*"%>
<%
AutoComplete autoComplete=new AutoComplete();
JSONObject jsonObject=autoComplete.autoComp();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script src="./JS/jquery-1.4.2.min.js"></script>
<script src="/JS/jquery.autocomplete.js"></script>
<script>
function lookup(inputString) {
alert("inside lookup");
if (inputString.length == 0) {
$('#suggestions').hide();
} else {
var object=<%=jsonObject%>
var data= JSON.stringify(object);
if (data.length > 0) {
$("#inputauto").autocomplete({
source: data
});
}
}
}
</script>
<head>
<body>
<div>
<form>
<div>
<br /> Enter Name to see autocomplete <input
type="text" size="30" value="" id="inputauto"
onkeyup="lookup(this.value);" />
</div>
</form>
</div>
</body>
</html>
This is my java file
package com.practise.autoComplete;
import net.sf.json.JSONObject;
public class AutoComplete {
public JSONObject autoComp() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("1", "peter");
jsonObject.put("2", "nadia");
jsonObject.put("3", "jack");
jsonObject.put("4", "areena");
return jsonObject;
}
}

Categories

Resources