Reputation: 737
i need to pass some var in my jsp... the value that i get of my servlet to my funciton in javascript... is this possible? i past my code:
<%@ 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">
<script type="text/javascript" src="js/lib/ext-all.js"></script>
<link rel="stylesheet" type="text/css" href="resources/css/ext-all-gray.css"/>
<link rel="stylesheet" type="text/css" href="resources/css/EstiloPrincipal.css"/>
<title>Tipo Papel</title>
<script>
Ext.require([
'Ext.form.*',
'Ext.form.field.File',
'Ext.layout.container.Column',
'Ext.window.MessageBox',
'Ext.fx.target.Element',
'Ext.panel.*',
'Ext.toolbar.*',
'Ext.button.*',
'Ext.container.ButtonGroup',
'Ext.layout.container.Table',
'Ext.tip.QuickTipManager'
]);
Ext.onReady(function() {
Ext.QuickTips.init();
var modificarRegistro = function(){
Ext.Ajax.request({
url: 'http://localhost:8080/MyMaver/ServletTipoPapel',
//url: 'http://lnxntf05:8080/MyMaver/ServletTipoPapel',
method: 'POST',
params: {
Funcionalidad: 'Modificar',
DPTipoPapel: Ext.getCmp('DPTipoPapel').getValue(),
DPIndicadorSoD: Ext.getCmp('DPIndicadorSoD').getValue(),
DPCaratula: Ext.getCmp('DPCaratula').getValue(),
DPFinicial:Ext.getCmp('DPFinicial').getValue(),
DPVinicial:Ext.getCmp('DPVinicial').getValue(),
DPFfinal:Ext.getCmp('DPFfinal').getValue(),
DPVfinal: Ext.getCmp('DPVfinal').getValue(),
DPVersCaratula: Ext.getCmp('DPVersCaratula').getValue(),
DPSpool: Ext.getCmp('DPSpool').getValue(),
DPTamPapel: Ext.getCmp('DPTamPapel').getValue(),
DPNumOptimo: Ext.getCmp('DPNumOptimo').getValue(),
DPFormularioCaratula: Ext.getCmp('DPFormularioCaratula').getValue(),
DPDescripcion: Ext.getCmp('DPDescripcion').getValue(),
DPTipoTrabajo: Ext.getCmp('DPTipoTrabajo').getValue(),
Entorno: Ext.getCmp('Entorno').getValue()
},
success: function(response){
var text = response.responseText;
alert("ha modificado un registro de la tabla tipo papel");
// process server response here
respuestaModificacion(text);
},
failure: function (){
alert("Desde failure");
},
exception: function (){
alert("Desde exception");
}
});
};
var top = Ext.widget({
xtype: 'form',
id: 'multiColumnForm',
collapsible: true,
frame: true,
//renderer:contenedor,
title: 'TIPOS DE PAPEL - MANTENIMIENTO',
bodyPadding: '0 0 0',
width: '80%',
//height: 800,
fieldDefaults: {
labelAlign: 'side',
msgTarget: 'side'
},
items: [cabeceraPrueba,customGroup,
simpleCombo,mensajesPrueba],
buttons: [{
text: 'Altas',
id:'botonAlta',
handler: function() {
altaTipoPapel();
}
},{
text: 'Alta Masiva',
id: 'botonAltaMasiva',
hidden: true,
handler: function() {
this.up('form').getForm().isValid();
}
},{
text: 'Consulta',
disabled: true,
id:'botonConsulta',
handler: function() {
consultarTipoPapel();
}
},{
text: 'Modificar',
disabled: true,
id:'botonModificar',
handler: function() {
modificarRegistro();
}
},{
text: 'Bajas',
disabled: true,
id:'botonBaja',
handler: function() {
bajaTipoPapel();
}
},{
text: 'Limpiar',
handler: function() {
limpiarCampos();
}
},{
text: 'Histórico',
hidden: true,
handler: function() {
this.up('form').getForm().reset();
}
}]
});
top.render(document.body);
});
</head>
<body leftmargin="3" topmargin="3" marginwidth="0" marginheight="0"
bottommargin="0">
<form name="form1" action="/MyMaver/GestorPeticiones" method="post" enctype="multipart/form-data">
<div id="campos"></div>
</form>
<%String name = (String)request.getAttribute("usuario"); %>
<%= name%>
</body>
</html>
I want to pass the var "usuario" to my script code....is this possible?
Upvotes: 0
Views: 149
Reputation: 1108557
JSP generates HTML code. JS is part of generated HTML code. Just write JSP code in such way that it generates exactly the desired HTML/JS code.
For example,
<script>
var usuario = "${usuario}";
</script>
Now open the JSP page in browser and do rightclick, View Source, and confirm if JSP has properly inlined the EL variable in the generated result in such way that resulting JavaScript code syntax is valid (i.e. quotes, semicolons and so on):
<script>
var usuario = "Some User Name";
</script>
Note: don't forget to take JS escaping into account with e.g. a custom EL function. It would otherwise break if the username contains a doublequote. Also note that scriptlets <% %>
is an oldschool way of writing JSPs and are discouraged since more than a decade. Just use EL as demonstrated above. The ${foo}
prints the first non-null attribute named "foo"
found in the page, request, session and application scope.
Upvotes: 1
Reputation: 12295
Yes it is...
<%String name = (String)request.getAttribute("usuario"); %>
var usuario = '<%=name%>';
Upvotes: 0