I have a button in a JSP page:
<button type="button" onclick="">Click me </button>
I want to perform the following jsp code:
<% session.setAtrribute("status","guest"); %>
when I press the button.
Is it possible to use JSP code on a button click and if yes how?
If you use a dot operator on a variable that is null, you get a null pointer exception. So you could use the dot operator on the string literal, and thus avoid explicitly checking for null: <% String x = request. getParameter("submit"); if("confirm".
The client (webbrowser) will in turn execute the HTML/CSS/JS. Knowing this fact, it should be obvious that the only way to let JavaScript access/invoke some Java/JSP code is to send a HTTP request to the server side. This can be done in several ways: using window. location to do a synchronous GET request, or form.
Do something like this:-
<%
if(request.getParameter("buttonName") != null) {
session.setAttribute("status", "guest");
}
%>
<FORM NAME="form1" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="buttonName">
<INPUT TYPE="BUTTON" VALUE="Button 1" ONCLICK="button1()">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!--
function button1()
{
document.form1.buttonName.value = "yes";
form1.submit();
}
// -->
</SCRIPT>
either use:
<input type="submit" value="click"/>
and set form action to some servlet/jsp page where you set you'r session attributeuse ajax in onclick button method.
JQUERY SAMPLE:
$.ajax({
url: "srvServlet", //or setJSP.jsp
success: function(){
alert ('ok');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With