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

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

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"

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

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.

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;
}
}

need to check/uncheck checkbox on page load depending on variable passed from controller in jsp

I am passing a variable to my jsp. If the variable is 1, I need to check a check box on page load. If it is 2, the checkbox will be unchecked on load. By default, the check box is unchecked.
I am using Spring MVC. Please suggest how can i do it using javascript/jquery.
Thanks.
Assuming the conditional variable is in form say yourForm, you can
set the check box in javascript and jsp as:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<script type="text/javascript" language="JavaScript">
function loadOnValues(){
var conditionalVariable = '<c:out value="${yourForm.conditionalVariable}"/>';
if(conditionalVariable == null || conditionalVariable == '2'){
document.getElementById("boxId").checked = false;
} else {
document.getElementById("boxId").checked = true;
}
}
</script>
</head>
<title>Jsp Title</title>
<body onload="javascript:loadOnValues();">
<form:form id="yourForm" modelAttribute="yourForm" method="post">
<tr>
<td>
<form:checkbox id="boxId" path="checkBoxName"> CheckBox Text
</td>
</tr>
</form:form>
</body>
</html>

Can not get innerHTML to work?

Why does my code not work? Shouldn't it print 222 to the element with ID p2?
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%#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>
<script type="text/javascript">
function compareData()
{
document.getElementById('p1').innerHTML = "111";
var inputs = document.getElemementsByTagName("input");
// why can't I display the "222" here??
document.getElementById('p2').innerHTML = "222";
}
</script>
</head>
<body>
<p id="p1"> a </p>
<p id="p2"> b </p>
<input type="button" onclick="compareData()" value="hello"/>
</body>
</html>
You have mis-spelled getElemementsByTagName. This results in a js error, thereby your code stops execution at that line.
Correct it as getElementsByTagName. Then your code would work fine.
Why you are using this? var inputs = document.getElemementsByTagName("input");
My fiddle
And you can use the way you are using like this : My fiddle but it was not working because Elements spelling is wrong document.getElemementsByTagName
It should be document.getElementsByTagName
You have a typo on this line:
var inputs = document.getElemementsByTagName("input");
What you want is probably this:
var inputs = document.getElementsByTagName("input");
It could be that the typo stops the execution of the script, and therefor the line after is never run.
Remove following line and it will work...
var inputs = document.getElemementsByTagName("input");

Categories

Resources