How to pass value from one jsp to other through window.open - java

I am getting gra value from one.jsp as follows
<script language="javascript">
function gotoAddPanelAction(elem)
{
var st=elem.value;
if(st!="")
{
Popup=window.open('SelectInterviewPannel2.jsp?gra='+st,'Popup','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes',400,400);
}
else
{
validateForm(document.frm);
}
}
</script>
I am retriving the value in SelectInterviewPannel2.jsp as follows
<td width="60%" class="txt-lable">
<Select name="grade" ><option value="" selected>Select</option>
<%
String gra = request.getParameter("gra");
if(gra.value=="Level 1") {
%>
<option value="E1">E1</option><option value="E2">E2</option><option value="E3">E3</option><option value="E4">E4</option></select>
<% } else {%>
<option value="M1">M1</option><option value="M2">M2</option></select>
<% } %>
</td>
My problem is in SelectInterviewPannel2.jsp if statement is not executing . I am getting only drop down box for select with no values in it.

if(gra.value=="Level 1") {
Shouldn't you use equals() method for string comparission
Some Suggestions:
Don't use java code on jsp.

First of all why this title: how to remove comma in integer in java it does not match with your question.
As an answer:
Instead of this
if(gra.value=="Level 1")
use this
if(gra.equals("Level 1"))
In java == Compares references, not values and to compare values of string you should use .equals(str).

If gra parameters does not exist , request.getParameter("gra") will return null and comparing using if(gra.equals("Level 1")) will throw out Null pointer exception . So you can try to use if("Level1".equals(gra)) to avoid the Null pointer exception when the gra parameters does not exist

Related

How to call a variable with JSP?

In JSP I have created a simple variable like this:
<%
if (request.getSession().getAttribute("user") != null) {
String status = "online";
}
else {
String status = "offline";
}
%>
In HTML I call the variable like this: ${status}
So the string "offline" or "online" would just be a CSS class, as you see in the code below:
<img class="profile-pic" src="../assets/img/reza.png" alt="Chin"> <i class="status ${status}"></i>Reza
So the whole code looks like this:
<%
if (request.getSession().getAttribute("user") != null) {
String status = "online";
}
else {
String status = "offline";
}
%>
<div class="user-section">
<img class="profile-pic" src="../assets/img/reza.png" alt="Chin"> <i class="status ${status}"></i>Reza
</div>
but from Intellij I get the warning:
Cannot resolve variable 'status'
How can I fix this?
You have a problem with scopes, when you declare a variable inside an if or else block you can use this variable just in that scope, you can solve your issue like so :
<%
String status = "offline"; // declare the variable outside the if else
if (request.getSession().getAttribute("user") != null) {
status = "online"; // if the condition is correct then assign "online"
}
%>
After your edit
It seems you are using the variable name with a wrong way, instead you have to use :
<i class="status <%=status%>">
Instead of :
<i class="status ${status}">
Per your updated code, you need to reference "status" like this in your HTML:
<div class="user-section">
<img class="profile-pic" src="../assets/img/reza.png" alt="Chin"> <i class="status <%=status%>"></i>Reza
</div>
"JSP" is processed "server side". It's output is HTML, which is then sent to your browser.
The Java variable "status" is set to the Java string "offline" or "online" on the server. You can use the JSP tag <%= %> to write its value into your HTML stream.

How to check JSP variable and hide if value is null/empty or some specific string?

I am creating a project where i am getting set data in JSP page from database.
if any field data value is null the jsp page is showing null but i do not want to show it on jsp page. please help. i am getting data from bean.
<%=p.getOffer()%>
<% String s = p.getOffer() %>
<% if (<%=s ==null) { %>
show nothing
If you are coding java inside a jsp, you need to use scriptlet tags(<% and %>). So if you are checking for conditions you need to open a scriptlet tag.
<%
String s = p.getOffer();
if (s != null && !s.equals("")) {
out.print(s);
} else { %>
<!-- s is either null or empty. Show nothing -->
<% }%>
What exactly do you want to show when the value is null? Anyway, your approach looks good:
<%=p.getOffer()%> // Here you print the value "offer". If you don't want to show it when it is null, remove this line
<% String s = p.getOffer() %>
<% if (<%=s ==null) { %> // the <%= is unnecessary. if(s==null) is enough
show nothing // show nothing, why not inverse the if and show something
Here another approach:
<%
String s= p.getOffer();
if(s != null){ %>
Offer: <%= s%>
<% }%>
That way you only print the offer when the variable is not null.
By the way: naming a String variable "s" is not recommended, call it "offer" or something to facilitate the reading.

Refreshing Page With A Parameter in JSP

I have a JSP page in my web application and i want to refresh the page with the parameter of option.
<select class="form-control combobox" name="education" >
<option value="" disabled selected hidden>Choose an education</option>
<option>English</option>
<option>French</option>
<option>Dutch</option>
<option>Spanish</option>
</select>
I want to use this parameter for querying my database again. Forexample;
<%
BookDAO bdao = new BookDAO();
for (TblBooks book : bdao.getAllBooks()) {%>
<tr>
<td><%=book.getTittle()%></td>
<td><%=boek.getAuthor()%></td>
<td><%=boek.getCategory()%></td>
<td><%=boek.getEducation()%></td>
<td><input type='checkbox' name ='chk1' /></td>
</tr>
<% }%>
I can get the parameter with request.getParameter("education") but how can i refresh page and query again with this parameter?
In Javascript, You can ask a web page to reload itself (the exact same URL) by:
location.href = location.href;
You can ask a web page to load a different location using the same mechanism:
location.href = 'http://www.google.com'; // or whatever destination url you want
You can get the value of a select control (which needs to have an id attribute):
var ctrl = document.getElementById("idOfSelectControl");
var selectedOption = ctrl.options[ctrl.selectedIndex].value;
So, if you update your JSP to give the control an id:
<select class="form-control combobox" id="education" name="education" >
you should be able to reload the page:
var educationControl = document.getElementById("education");
var selectedOption = educationControl.options[educationControl.selectedIndex].value;
location.href = "http://mysite.example/mypage?education=" + selectedOption;
By the help of Jason i have solved it. I added an onchange event to my select tag
onchange="changeThePage()"
Then i defined a javascript function.
function changeThePage() {
var selectedOption = "book.jsp?education=" + $("#education option:selected").text();
location.href = selectedOption;
}
Then i got the parameter by
String education = request.getParameter("education");
Then i added String education as parameter to my bdao.getAllBooks(education)) method for to query.
'
<script>
function abc(){
var yourSelect = document.getElementById( "ddlViewBy" );
var val = yourSelect.options[ yourSelect.selectedIndex ].value;
//window.location.href=window.location.href;
alert(val);
window.location.replace("index.jsp?name="+val);
}
</script>
<select id="ddlViewBy" onchange="abc()">
<option>select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>
<%
String name=request.getParameter("name");
if(name!=null){
%>
<table>
<tr>
<td>author<%=name%></td>
<td>book name<%=name%></td>
</tr>
</table>
<%
}
%>
Hi,
try this, this will be helpful,
when you select a value it will call abc, function and then you are assigning that value in scriptlet..and also page is refreshing. in scriptlet you can access that value using request.getParameter("name").
now you can write your code inside scriptlet..hope helpful for someone.. :)
thanks...

HTTP 500 in redirecting to the same jsp

i am having f.jsp which returns a lists of ages(1..100) and genders(m/f) and a button GO and put it in comboboxes and i have f_m.java which is servlet which took the selected items from comboboxes and select an certain item from database and put it in table in f.jsp .. now my problem is when trying to print the table in f.jsp and HTTP 500 appears so what can i do .. here is my code
f.jsp
<% List<String> sex = (List<String>)request.getAttribute("sexList"); %>
<% List<String> age = (List<String>)request.getAttribute("ageList"); %>
<form method ="GET" action="f_m" >
<html>
<body>
<table>
<tr>
<td>Gender:</td>
<td><select name="sex">
<%for(String item : sexList) { %>
<option value="<%=item%>"><%=item %></option><%}%>
</select>
</td>
<td>age:</td>
<td><select name="age">
<%for(String item : maritalStatus) { %>
<option value="<%=item%>"><%=item %></option><%}%>
</select>
</td>
<td><input type="submit" value="Go" name="Go"></td>
</tr>
</table>
</form>
</body>
</html>
and f_m.java
String gender = request.getParameter("sex");
String age = request.getParameter("age");
if(request.getParameter("Go") != null){
// i want to go to f.jsp to print the table
}
You must set the request attributes in the servlet before forwarding to the jsp. I suppose it could be something like :
// prepare the attributes
int ageList = new int[100];
int sexList = new String[]{"m", "f"};
for (int i=0; i<100; i++) { ageList[i] = i + 1; }
// put them in request
request.setAttribute("ageList", ageList);
request.setAttribute("sexList", sexList);
// and forward to the jsp.
request.getRequestDispatcher("f.jsp").forward(request, response);
You have two lists: sex and age. When you are trying to populate the select, you asked:
<%for(String item : sexList) { %>
Where is declared your sexList? May be you want to iterate through sex list? I mean, try:
<%for(String item : sex) { %>
And the same for second list:
<%for (String item : age) {%>
You must be more explicit what are you trying to do in your Servlet. I assume your main scope is to populate two selects in a form depending on two lists: sex and age. After this you want to take 2 selected values and pass them to servlet, the servlet will make a call to a database depending on these two values and will return a row from the table. After this you want to print this row in your f.jsp. If so, get the values from the select (and populate them right, like i mentioned below) in your servlet:
String sex = request.getParameter("sex");
String age = request.getParameter("age");
//method to call database, i.e List<Persons> list = DBHelper.checkSomething(sex, age);
if(...){
RequestDispatcher rd = request.getRequestDispatcher("f.jsp");
request.setAttribute("list", list);
rd.forward(request, response);
}
And i strongly recommend you to print your data based on a View. If you want to know more, read about MVC patterns.

how to check null value in html textbox?

I have a JSP page where i am fetching value from html textbox.
I want to insert that value into MySQL database.
I want if textbox in empty,null,zero and undefined then insert 0 in database otherwise actual value insert in database.
Here is my code.
Strins s1=request.getparameter("edate");
s1="1990-07-16 09:12:45"
int s4=0
<script>
var a=<%=s1%>
if(a==null || a==undefined || a=='' || a==0){
<% psmnt.setInt(6,s4); %>
}
else{
<% psmnt.setString(6,s1); %>
}
</script>
Is your texbox <texarea> or <input type="text">?
If you are using <input type="text">, put default value on it:
<input type="text" value="">
While for <textarea>, <textarea></textarea> should give your default value ""
So that you only need to check
if(a==''){
......
}
Or if you didn't allow white space too, do this:
if(a=='' || a.trim() ==''){
......
}
You can use this
<script>
if(typeof a == 'undefined' || a == 0) {
// Insert db 0
}
</script>

Categories