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"
Related
As described in the problem
I create two buttons. The one is used to add forms,the other one is used to submit data.However, after inputting data when i clicked on the button of submit,it didn't work
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GA'S WEB APPLY</title>
</head>
<body>
<script>var i=1</script>
<p>test is ing!</p>
<hr>
<div>
<form action="/webTest/TestWeb" method="post">
<input type=button onclick="document.body.insertAdjacentHTML('beforeEnd','<input type=text name='+i+' value='+i+++'> ')" value=add />
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>
Problem in your code is with '' quotes as well as ,whenever you append new input it appear out of <form></form> tag , so whenever you submit your form that value doesn't passed to your backened page and will always return null. Instead do like below :
var i = 1
$(document).ready(function() {
//location to add new inputs
var to_Add_at = $(".inputs");
var add_button = $("input[name='add']");
//on click of button
$(add_button).click(function(e) {
e.preventDefault();
$(to_Add_at).append('<input type=text name='+i+' value='+i++ +'>'); //add input box
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<form action="/webTest/TestWeb" method="post">
<input type="button" value="add" name="add" />
<div class="inputs"></div>
<input type="submit" value="submit" />
</form>
As I can not see the backend data or anything else besides the code. I would say
1. File location wrong?
2. Page language should be javascript
3. Add quatations "" around value = add
This is my jsp page
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
function getChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
return k;
}
</script>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain">
<h3>${message} </h3>
<br> <br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}"> <br><br>
<input type="button" value="Overwrite ?" onclick="choice=getChoice()"/>
<input type="submit" value="Submit ?"></form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center>Click me to upload a file <br><br>
</center>
</body>
</html>
In the above code, i want to get the result of confirm popup in to a variable "choice". How to do that ?
Modified code :
bean class :
package com.fss;
public class Choice {
private String choice;
private String fname;
private String fcontent;
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getFcontent() {
return fcontent;
}
public void setFcontent(String fcontent) {
this.fcontent = fcontent;
}
}
this is my jsp page :
<html>
<head>
<script>
function getUserChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
$('#choice').val(k);
return k;
}
</script>
</head>
<body>
<center><h3>${message} </h3></center>
<br>
<center><form method="POST" action="saveagain">
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}">
<input type="hidden" name="choice" id="choice"> <br><br>
<input type="submit" value="Overwrite ?" onclick="getUserChoice()"/></form></center>
<center>Click me to upload a file </center>
</body>
</html>
This is my Controller class:
#RequestMapping(value="/saveagain",method = RequestMethod.POST)
public ModelAndView fileUploading2(#ModelAttribute("ch") Choice ch) throws IOException{
String ch1;
String message="";
ch1=ch.getChoice();
System.out.println("popup result "+ch1);
return new ModelAndView("fileUpload","message",message);
}
fname and fcontent values are reflecting in controller. why is choice value not reflecting in "ch1" ?
With your approach, you can use javascript in the getChoice() function to put the return value(true/false) as an hidden element value and retrieve on the server side
function getChoice() {
var k=confirm("Do you want me to overwrite the existing file ?");
$('#hidden_choice').val(k);
}
<!-- HTML element -->
<input type="hidden" name="choice" id="hidden_choice" />
<input id="button_id" type="button" value="Overwrite ?" onclick="getChoice()"/>
On the server side, you can use #RequestParam to retrieve the value. Following is the Example code:
#RequestMapping(value="/saveagain",method = RequestMethod.POST)
public ModelAndView fileUploading2(#RequestParam("choice") Boolean ch) throws IOException {
// do your thing with choice variable here
System.out.println("Choice is: " + ch);
}
If the boolean value does not work, try with String and convert it to Boolean value manually.
You can create a variable such as cofirmstatus in the form.
<input type="hidden" name="cofirmstatus" id="cofirmstatus">
And set the value as below
function getChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
$('#cofirmstatus').val(k);
return k;
}
So, once the form is submitted, this value will be available on the server side
EDIT:
I created the JSP as follows. No changes required on server side.
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function submitForm(){
var k=confirm("Do you want me to overwrite the existing file ?");
document.myform.choice.value=k;
document.myform.submit();
}
</script>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain" name="myform">
<h3>${message} </h3>
<br> <br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}"> <br><br>
<input type="hidden" name="choice" id="choice"> <br><br>
<input type="button" value="Overwrite ?" onclick="javascript:submitForm()"/>
<input type="submit" value="Submit ?"></form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center>Click me to upload a file <br><br>
</center>
</body>
</html>
Output on server for both the actions:
popup result true
popup result false
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
I work with Spring MVC. I have a dynamic web project Eclipse
Structure folder is
I have the following jsp actualizarCorreo.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<title>EusurveyAdmin menu princiapl</title>
<script type="text/javascript" src="/resources/js/properties.js"></script>
<script type="text/javascript">
function pruebaB()
{
alert('en prueba');
}
</script>
</head>
<body>
<c:set var="pageNumber" value="${pageNumber}" />
<table class="actualizarCorreos">
<tbody>
<a href="#" onclick="prueba()"; return false;">
<input type="radio" onclick="prueba()" class="check" id="id1"
name="id1" value="true" />
<spring:message code="label.ModificarCorreos" />
</tbody>
</table>
</body>
In this jsp I load another js
<script type="text/javascript" src="/resources/js/properties.js">
This js is the path WebContent/resources/js.
properties.js is
alert('cargado properties');
function checkConfirmationPage()
{
alert('en check');
if ($("#conflink").is(":checked"))
{} else {};
}
function prueba()
{
alert('en prueba');
}
I call function prueba from actualizarCorreo.jsp when a radio button is clicked
<input type="radio" onclick="prueba()" class="check" id="id1"
name="id1" value="true" />
<spring:message code="label.ModificarCorreos" />
I don't see the alert message function prueba.
What is it wrong?
Try this
<script type="text/javascript" src="../resources/js/properties.js"></script>
or use the below as reference and find the exact path:
/ = Root directory
. = This location
.. = Up a directory
./ = Current directory
../ = Parent of current directory
../../ = Two directories backwards
Or directly get the context path to fetch the .js
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/properties.js" >
</script>
While using Spring framework in mvc you have to provide resources path as below in your dispatcher-servlet.xml.
< mvc:resources mapping="/resources/**" location="/resources/" />
And then that folder will be allowed for resources. You can call them as
< script type="text/javascript" src="../../resources/js/properties.js" >
</ script>
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.