cannot convert data to upper case using `request.getParameter` - java

<%String dest = request.getParameter("destination").toUpperCase();%>
Hello...
I got a little bit problem here. I am using the above code to get value from form. When use the code without toUpperCase(), it was a success. But, when I add toUpperCase() I got HTTP Status 500 - An exception occured processing JSP page.

When you get value null from request.getParameter("destination"), apply toUpperCase() to a null value gives an error.
Try to do like this:
<%String dest = request.getParameter("destination");
if(dest!=null){
dest = dest.toUpperCase();
}
%>
The request.getParameter() returns String value or a null value from client.

More than likely, request.getParameter("destination") is returning null in your code, which would be why it's throwing an error. If the parameter is not found, then null is returned, otherwise a String is returned.
So you'll want to verify that it's not null
<% String dest = request.getParameter("destination");
if(dest != null) {
dest = dest.toUpperCase();
}
%>

Related

Apache Camel header set to null after performing a get

What could cause a header to be set to null as a result of getting it from the exchange?
My current workaround is to set the header again after reading but this is terribly hacky.
Example:
String header = exchange.getIn().getHeader("headerKey", String.class);
String theSameHeader = exchange.getIn().getHeader("headerKey", String.class);
System.out.printf("header is %s.\n", (header == null) ? "null" : "not null");
System.out.printf("theSameHeader is %s.\n", (theSameHeader == null) ? "null" : "not null");
Output:
header is not null.
theSameHeader is null.
Are you header a String type, it does however smell as if the header type is streaming based, and then you end up as what is described in this FAQ:
http://camel.apache.org/why-is-my-message-body-empty.html

Null pointer exception in passing checkbox value

I made an index page in which you take the value of checkbox and pass it on to a file named AddToWork.java but it is showing null pointer exception. There is some problem in passing the value of the checkbox. Kindly help. Here is the code snipped for index page
<td>
<center>
<form action="addtowork?id2=<%=mail.getTempToken()%>" method="post">
<input type="submit" value="Add to my work">
<input type="checkbox" name="flag" value="flag">High Priority</form>
</center></td>
for AddToWork.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EmailDesc mail = new EmailDesc();
String imp = new String();
imp = (String) request.getParameter("flag");
String thisid = request.getParameter("id2");
Home home = new Home();
User user = new User();
user = (User) request.getSession().getAttribute("user");
mail = home.getEmail(thisid, user);
home.givePermanentToken(mail,thisid);
if (imp.equals("flag")){
System.out.println("Priority Changed to " + mail.getPriority() + "!");
}
response.sendRedirect("index1.jsp");
}
If I remove the if statement in addtowork.java, the code runs perfectly.
it is because your "imp" Object is pointing to nothing (null) & it is throwing an exception. use "Yoda notation" like so
if("flag".equals(imp)){
// your code
}
this removes the possibility of getting a null pointer exception
Case 1: name.equals("Java") Compare unknown value with known value.
We are comparing name(unknown) value with another string Java(known) value. name will be decided based on some database call, calling another method, etc... It may possible you get null value of name and possible chances of java.lang.NullPointerException or you have to check explicitly for null value of name.
Case 2: "Java".equals(name) Compare known value with unknown value.
We are comparing Java(known) value with another string name(unknown) value. Same way name will be decided based on some database call, calling another method, etc... But equals and equalsIgnoreCase method of String will handle the null value and you don't have to check explicitly for null value of name.
In your case
/* You are getting `null` for variable `imp` */
imp = (String) request.getParameter("flag");
Change
if (imp.equals("flag")){
System.out.println("Priority Changed to " + mail.getPriority() + "!");
}
to
if ("flag".equals(imp)){
System.out.println("Priority Changed to " + mail.getPriority() + "!");
}
Case I : When you are submitting the form with checking the check box , it will work because the value is set in request parameter flag
Case II : When you are submitting the form without checking the "priority" check box then the request parameter sets to null and later you calling the equal method on null on if condition. so please use
if("flag".equals(imp))
Note :- It's bad practice to create string using new
String imp = new String(); //bad don't use this
String imp = ""; //use in this way

Querystring without values

Im new to JSP and I am using a flag in my application like the URL below:
http://localhost/MyApp/result.jsp?params
How do I get that flag in the landing page?
You can use request.getQueryString() to access the raw query string (everything after the ? up to the first #, if there was one) as it was in the original URL.
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
if (paramName.equals("params")) {
....
}
}
If you do like this you don't have to specify a value for the parameter.
You can check this example
request.getParameter("params") will return you a blank-string.
To set a boolean flag, you can simply do.
boolean flag = request.getParamater("params") != null;
In your result.jsp file, you can get the param value within the scriplet tag <%...%> like this :
<%
String params = request.getParameter("params");
%>
Also, as DnR quoted in the comments, you will have to place '=' after the param flag even if you don't want to associate a value with this flag.

Want to pass the java value into javascript function in jsp

I am trying to pass a string value to a JavaScript function by taking from request parameter in JSP, in my struts based project. here is the code:
<%
String timeVal = "Not found";
if(request.getAttribute("myDate")!=null){
timeVal= (String)request.getAttribute("myDate");
}
%>
and then pass it in function as parameter
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', <%=timeVal %>)">Save</html:submit>
Where the JavaScript function is
function SubmitPage(action, aa)
{
alert("Date is ...." + aa);
}
But when i try to run this it gives me an error
HTTP Status 400 - Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
With message on web page.
Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
Thanks in advance.
EDIT Here is stack trace
[ERROR] DispatchAction - -Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
it's work for me :
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('<%=timeVal %>')">Save</html:submit>
('<%=timeVal %>') // between single Quotation
Rather using that i will advise you to use value like this in your JavaScript function
var tt = <%=(String)request.getAttribute("myDate")%>
alert(tt+ "Done this....");
Hope this will help you.
Use '<%=timeVal %>' instead of <%=timeVal %> in Javascript method:
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', '<%=timeVal %>')">Save</html:submit>

url harvester string manipulation

I'm doing a recursive url harvest.. when I find an link in the source that doesn't start with "http" then I append it to the current url. Problem is when I run into a dynamic site the link without an http is usually a new parameter for the current url. For example if the current url is something like http://www.somewebapp.com/default.aspx?pageid=4088 and in the source for that page there is a link which is default.aspx?pageid=2111. In this case I need do some string manipulation; this is where I need help.
pseudocode:
if part of the link found is a contains a substring of the current url
save the substring
save the unique part of the link found
replace whatever is after the substring in the current url with the unique saved part
What would this look like in java? Any ideas for doing this differently? Thanks.
As per comment, here's what I've tried:
if (!matched.startsWith("http")) {
String[] splitted = url.toString().split("/");
java.lang.String endOfURL = splitted[splitted.length-1];
boolean b = false;
while (!b && endOfURL.length() > 5) { // f.bar shortest val
endOfURL = endOfURL.substring(0, endOfURL.length()-2);
if (matched.contains(endOfURL)) {
matched = matched.substring(endOfURL.length()-1);
matched = url.toString().substring(url.toString().length() - matched.length()) + matched;
b = true;
}
}
it's not working well..
I think you are doing this the wrong way. Java has two classes URL and URI which are capable of parsing URL/URL strings much more accurately than a "string bashing" solution. For example the URL constructor URL(URL, String) will create a new URL object in the context of an existing one, without you needing to worry whether the String is an absolute URL or a relative one. You would use it something like this:
URL currentPageUrl = ...
String linkUrlString = ...
// (Exception handling not included ...)
URL linkUrl = new URL(currentPageUrl, linkUrlString);

Categories

Resources