JSP Form is picking the edited value in FireBug - java

I have a form to be submitted in a jsp file. When submit, the form uses a value which is coming from a hidden value, say id.
<input type="hidden" value="1234" name="Id">
Prior to submit, I am able to run the firebug and edit this input value to a non-numeric value (e.g abc). now when I submit the form, apparently the form picks the new value and throws error (id cant be non-numeric).
How can I solve this issue? Thanks,

might it's help for you
you can set session variable for id and remove hidden field .
and get session variable where you wanted .
example :
JSP Sessions
If you are programming the site, it is very helpful to be able to associate some data with each visitor. For this purpose, "session"s can be used in JSP.
A session is an object associated with a visitor. Data can be put in the session and retrieved from it, much like a Hashtable. A different set of data is kept for each visitor to the site.
<%
session.setAttribute( "id", yourid );
%>
<HTML>
<BODY>
Continue
</BODY>
</HTML>
The SaveName.jsp saves the id in the session, and puts a link to another page, NextPage.jsp.
NextPage.jsp shows how to retrieve the saved name.
<HTML>
<BODY>
Hello, <%= session.getAttribute("id") %>
</BODY>
</HTML>
If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of.

Related

Extracting and processing textarea value form without changing the page

I have a JSP file in which there are two textareas and a submit button all in a form ;
<form action="" method="post">
<textarea name="inConsole" id="in" cols="100" rows="10"> </textarea> <br/>
<input type="submit" name="Send Command"/> <br/>
<textarea name="outConsole" id="out" cols="100" rows="10"></textarea>
</form>
this page is supposed to work like any SQL program. so the user types a command in the first textarea and clicks submit, then the value of textarea will be extracted to a field and a method will take care of the command and return a log (1 row inserted, error:bad syntax etc) which will be displayed in the second textarea.
I know how for example make a login page and send data and redirect user to a new page(new jsp) file if user pass is correct.
what I can't find is how can I do all the things that I said above without going to a new page while using form action.
I have checked other questions that linked the action attribute to a servlet which was confusing for me( the way that a servlet was called). I'm looking forward to use a simple scriptlet for this purpose like the one I used for my login page:
<%
DatabaseLoginTest dbLogTest = new DatabaseLoginTest();
if (dbLogTest.DBLoginChecker(request.getParameter("user"), request.getParameter("pass")) == true){
%>
<p>Login Successful</p>
<% } else { %>
<p>Login Failed</p>
<% } %>
also I'm aware that adding java scripts(not Javascript scripts:) ) to html isn't a good practice(and the reasons for it) but I think this might be easier for a simple program that I'm working on.
p.s: I'm using tomcat and Intellij for developing this web application
and I have made a custom SQL so I only need the code that gives me the textarea value and the one that sets the other one's value
Update: now I know I should use javascript but I don't know how can I send the data extracted by javascript to a java method.
If you want to do this while remaining in the same page, you have to use Javascript. This is because if you want the server to be able to re-render the page, there has to be a page refresh.
You would need to write onClick handler for the submit button and make a Ajax call to your server to a specific URL with the user input. This URL would serve the data needed for the necessary UI changes.
You can use a scriptlet to generate the HTML that would be shown in the webpage but this would only suffice for a simple use-case and it would be a lot simpler if, say, your service returned just the data required to make the UI change and actual UI change is handled by the JS.
Also,I don't think it is a bad practice to embed JS in HTML. Sure, you can optimize this by including a JS source file but that's a separate optimization.

How can i use Javascript variable as argument in java code in JSP? [duplicate]

This question already has answers here:
Assign JavaScript variable to Java Variable in JSP
(7 answers)
Closed 8 years ago.
I want to send code title and price as an argument to the book object but gives illegal start expression error.
function addrecord() {
code = document.getElementById('code').value;
title = document.getElementById('title').value;
price = parseFloat(document.getElementById('price').value);
<% Book book = new Book(%> code <% ,%> title <% , %> price <% );
bookdb.addRecord(book);
%>
document.getElementById("code").value = "";
document.getElementById("title").value = "";
document.getElementById("price").value = "";
}
You are misunderstanding where java and javascript run.
You use <% and %> as if that sends values to the server, which it does not. those tags are used only inside the jsp to start snippets of java code. This only works while you are generating the page. Once your html is at your browser, these tags will not do anything.
Highlighting important keywords, this is not intended as sarcarsm
When you request a jsp page with your browser, the browser opens a network connection to the server. Think of these 2 (browser, server) as living in 2 completely different places.
The server then runs the java code. When it executes, it generates an html page.
This html page is sent back across the network connection to the client (your browser)
Your browser will receive this fully formed html page and use it to create it's view. On this page you can run javascript but the javascript will only run on the client (browser)
To get any variable back to the server again, you will need to either: get or post a form (using a submit inside a form tage, or use JavaScript post request like a form submit) , do an ajax call (http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml) or have the user click a link (a href="...your page").
The easiest way is to do:
<form method="POST" action="your/page/url">
<input type="hidden" name="someName" value="someValue" />
<input type="submit" />
</form>
read some of the links and make sure you keep in mind, your browser and the server are separated, so running javascript in your browser will not reach the server.
See here: Assign JavaScript variable to Java Variable in JSP
As that states, it's really not possible as the JSP executes on the server, and the javascript executes later in the browser.

expiring the session of user if new tab is opened in browser using struts2 interceptors

My application is in struts2.
I have requirement that I am storing my application details in session. I cannot remove those attributes from session. After I log-in I have to choose an object to work on. Now I open a new tab and choose another object for working. Due to which my first object values are over written. Hence if in my first tab I do some work wrong information is updated for that action.
This is how I am planning to solve the issue.
I am trying to set a value in setter method in interceptor class but I cannot access that value in my index.jsp.
This interceptor is called on all the actions and index.jsp is also included in every jsp.
I will maintain a hashtable which will store the userid and a random string of 40 characters. This id will be unique and I will update the userid after comparing the value present in jsp to value stored against the value stored in hastable.
If I find that value matches then I will generate another value and store in jsp and hastable. If value does not matches I will destroy the session.
Kindly advice how can I proceed or is there any other work around to achieve the same.
code
Interceptor class
String strFrom = (String)aContext.get(ServletActionContext.ACTION_NAME);
HttpServletRequest httpReq = (HttpServletRequest)aContext.get(ServletActionContext.HTTP_REQUEST);
HttpSession session = httpReq.getSession();
String sessionValue = (String)httpReq.getSession().getAttribute("sessionValue");
String test = httpReq.getParameter("sessionValue");
if(strFrom.equals("ValidateUser")){
// Do nothing
}else if(sessionValue == null && strFrom.equals("HomePage")){
session.setAttribute("sessionValue", getRandomString(20));
}else if (sessionValue.equals(session.getAttribute("sessionValue"))){
session.setAttribute("sessionValue", getRandomString(20));
}else{
httpReq.setAttribute("message","Session Expired. Please Login Again");
return "loginAgain";
}
index.jsp
<body>
<s:hidden id="hidBidType" value="%{#session.tenderBidType}"/>
<s:property value="%{#session.sessionValue}"/>
<s:hidden name="sessionValue" id="sessionValue" value="%{#session.sessionValue}"/>
</body>
login.jsp
<form name="loginPage" method="post">
<s:hidden name="sessionValue" id="sessionValue" value="1"/>
</form>
For your randomly generated value of 40 characters, why not use timestamp?
Well, I don't see much advantages of your aproach (but I could be wrong, of course). You still can register a SessionListener and check if the user has session, cant't you? So, do you realy need to do all this kind of stuff is up to you, but I wouldn't do it that way - Interceptor for all pages, store value in the page, check if is the same... Just register one SessionListener and put your user ID or some otrher user's unique value in session, compare and destroy session (or whatelse you need).
Hope this helps.
EDITED
I'm working on a web application with a similar requirements. What we do (and I'm sure is not the best choice) is after logging open new window without navigation bar and close the loggin window. Then we capture all the events on the window and disable many features like ctrl+tab, right click etc. In this application the user is not allowed to open more than one window. I repeat - this is probably not the best choice. Just an idea.

JSP page with dynamic html

I've been trying to write a website in which all navigation is handled by hiding and showing divs. It is my understanding that this method is called Single Page Interface. This has worked for simple designs in the past but my current task is starting to become very troublesome using this method. How would I go about replicating the same behavior but instead of hiding and showing divs I can just have a main container div that is then populated with the desired html from the server?
Example:
<script>
$("#button").onclick(function() {
$("#a").show();
$("#b").hide();
});
</script>
<html>
<body>
<div id="a" style="display:none;">A: SOME HTML</div>
<div id="b" style="display:block;">B: SOME HTML</div>
<button id="button">Change to A</button>
</body>
</html>
(note this is a very rough example of white I'm trying to do)
But I would like the contents of a container div to change from "B" to "A" via some jsp
Could anybody point in the correct direction?
Further Explanation:
Maybe I can clarify a little better. So when the user loads the page they are presented with a section that has a table of all the existing files in a database. The user can select a file from the DB list to rename or copy. If the user wishes to rename a file, for example, they would be presented with a new display (all within the same "Tab") which will have a set of fields populated for the file that they have selected and a set of empty fields in which they can specify the new file name. Currently this changing of displays is handled by showing and hiding divs, but I would like to retrieve the html that I want to display from the server and present it. Basically mimicking the hiding and showing of divs.
As it's not completely clear to me what you're trying to do I'll give you some options:
Replace the content of a element on your page see
Since you're using a JSP, you can use server side logic to display certain fragments
You're using a JSP, use that to render some server side content
Ad 1:
(assuming jQuery) $('body').load('serverSide.html'); see http://api.jquery.com/load/
Ad 2:
<% if ("a".equals(request.getParameter("aOrB"))) { %>
<jsp:include page="/a.jspf">
<% } else { %>
<jsp:include page="/b.jspf">
<% } %}
Ad 3:
<%= request.getAttribute('content') %>
Hope that helps

Inter Portlet Communication page submittion

I am using java liferay portal, in which there are multiple portlets. I want to create a portlet with a form that when it is submitted the data is retrieved and the specific result is shown in some other page portlet. But unfortunately these things are not going in the way.
I was thinking of using sessions but 2 problems arrised.
javascript value assignment to java variable.
if the values are passed to the page on which the specific portlet is placed, that portlet doesn't get the values.
Then I heard the concept of Inter Portlet Communication(IPC), and took some help from "liferay in action" but there the code works if both the portlets are placed on the same page, and my requirement is that one portlet is placed on first page and when the form is submitted it is redirected to the second page, to the second portlet for getting the parameters. I tried more example but its not working in my way.
I have found another way of, a relatively easiest, just tried that wiki from liferay
As i understood, you have some JavaScript parameters which you want to pass to the next page. You can, though, do it with APPLICATION_SCOPE of PortletSession and you can solve the problem of converting JS params to Java by placing the values in a input. If these input vars aren't supposed to be written by the user and you take them from somewhere else, you can make the input hidden:
In your jsp:
<form>
<input type="hidden" id="myinput1" name="in1" value="">
<input type="hidden" id="myinput1" name="in2" value="">
</form>
<script>
var a = "avalue";
var b = "bvalue";
document.getElementById("myinput1").value=a;
document.getElementById("myinput2").value=b;
</script>
Then submit the form when you need to. Next, you'll be able to do like this in the ProcessAction method of the portlet:
String a= request.getParameter("in1");
String b= request.getParameter("in2");
PortletSession session = request.getPortletSession();
session.setAttribute("a", a , PortletSession.APPLICATION_SCOPE);
session.setAttribute("b", b , PortletSession.APPLICATION_SCOPE);
In the other portlet, you can find it by calling
session.getAttribute("a",PortletSession.APPLICATION_SCOPE);
This, of course, if you can't simply place them in the next page's url.
As far as I know IPC does indeed only work between portlets on the same page. Also the portlet specification doesn't provide a generalized mechanism for switching pages so you can only use portal vendor specific ways to achieve that. But using public render parameters and a correctly constructed Liferay URL to another page you should be able to achieve the result you want: http://www.liferay.com/web/guest/community/forums/-/message_boards/message/1207858

Categories