Getting parameter from forward [duplicate] - java

This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 8 years ago.
I'm working on a project and I have a problem. I send a parameter from a jsp page via forward to another jsp page and although it has the value that I declared (I printed it and it works), when I use it in an if clause the code doesn't work. Here is the part of the code:
String parameter = request.getParameter("loginparam"); //loginparam is the the name of parameter in the forward statement whick has value "now"
if(parameter == "now"){
prints the jsp page
}else{
response.sendRedirect("mainpage.jsp");
}
It always redirects me in mainpage although when I print parameter it has the "now" value.

Try with,
if(parameter.equals("now")){
And Read

Related

Comparing Text Isn't Working in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
So, I'm trying to do something really simple, and that's check if a password equals something in my SWT application. So, my code was this:
if (passwordBox.getText() == "test") {
passwordBox.setVisible(false);
}
However, in my application, which uses text fields marked as passwords (with that variable), it will not fire that when I click a button. I already have button handling working, since I have an else statement that fires, but this will not fire when the password box obviously equals "test". What's wrong here?
Strings are always compared by equals().
if(("test").equals(passwordBox.getText())) {
passwordBox.setVisible(false);
}

Multiple Http Servlets string path mapping [duplicate]

This question already has answers here:
Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
(7 answers)
Closed 6 years ago.
I'm want to map these two different servlets (using addServletWithMapping):
"/soccerapp/teams/*"
"/soccerapp/teams/*/players
but the second is obviously never reached, is any other special char that I can use in the string path to solve the problem?
The wildcard in the middle of the path is not evaluated. You can see this answer https://stackoverflow.com/a/15386616/1630604 for a complete description how the path are evaluated.
Anyway, you cannot map two Servlet to one URL.

Comparing strings in onPostExecute (resulting from mysql operation) [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I have been trying to connect to a mysql database and it was succesfull. In the onPostExecute method, I get a result which seems to me exactly the same as the real input. In this example the emailadress jb#jb.jb. Logcat shows both the real input as the result. Yet my code doesn't agree that it is the same. Any advice?
EDIT: This link apparently gave the answer: answer on the question
You have to use String.equals() instead of == to compare Strings. check this link

Subtracting two dates with time in JSTL [duplicate]

This question already has answers here:
display Date diff in jsp
(3 answers)
Closed 8 years ago.
Let's say I have two variables called AdmittedDate and currentDate. They contain values like
"2014-10-13 14:47:44.0"
I want to cast them into dates and subtract them and show them in webpage. Can someone help?
JavaScript
var differenceInMin=(new Date(AdmittedDate ).getMilliseconds()-new Date(currentDate).getMilliseconds())/60000;
var diffInHrs=differenceInMin/60;
var diffInDays=parseInt(diffInHrs/24)+" "+parseInt(diffInHrs)+":"+((diffInHrs-parseInt(diffInHrs))*60);

Java substring issue [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
EDIT: Thank you, PakkuDon
instead of using "==" I must use ".Equals()"!
I'm trying to implement a chat-command system though I've encountered troubles in differentiating between standard chat and command chat..
Focusing on just one command for now, `highlight
the output whenever I type in `highlight is:
highlight
`highlight
Here's my code:
String cmd = InMessage.message.substring(0, 10);
System.out.println(cmd);
System.out.println("`highlight");
if( cmd == "`highlight" )
{
... cancel chat packet and proces command
}
and yet the if statement returns false.
What's going on here? Anything I've done is wrong?
You have a number of problems. Firstly, that substring is going to throw an exception if the user types something that's less than 10 characters. Secondly, you're using == where equals would work better. But you don't need either of these things. You could just use
if (InMessage.message.startsWith("`highlight")) {
// whatever
}

Categories

Resources