javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You are not suppose to change the username in the session. You put it once when you log in. You don't set it every time you load a page.
Of course it is null every time you do this:

String username = request.getParameter("username");
session.setAttribute("username", request.getParameter("username"));

If you don't pass the "username" as parameter in the request the first call will return null and then you will set null in the attribute.
The first call is completely useless if you have already put the username in the session after login.

So once you login put the username in the session and that's it. You don't need anything else. All you need to do next is get it from the session whenever you need it.
And after logout set it to null

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If you put it in the session it shouldn't got away. You probably put it in the request, so better show some code.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post code and the name of the file.
Also you probably need to set the classpath.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

A library for formatting dates. Every time I do that I have to create a new SimpleDateFormat, declare the format and use its methods. Have a class that does that. Also you can have a validation method that takes as argument a String and a format and checking if the String represents a Date. Also you can have a converter that converts java date formats to sql formats:
"dd/MM/yyyy HH:mm : ss" ---> "DD/MM/YYYY HH24:MI : SS"

A library for reading input from the keyboard.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Your code is a mess. What are you trying to accomplish?
Also you posted code with no code tags, without specifying your problem and where you get it.
Simply by saying it doesn't work won't get you any help

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

From the API this will return numbers from 0 to 9: rand.nextInt(10) So this:

int num = rand.nextInt(10)+1;
System.out.println("Generated Random Number between 0 to 10 is : " + numbers[num]);

will give you numbers from 1 to 10. The array has indexes from 0 to 9. So you will get an exception.

Also from what I understood we don't want numbers from 1 to 10, but to display randomly one of these: "one", "two", "three", ...
So it wouldn't matter even if the numbers generated were from 53 to 62 as long they are mapped to one of the String we want to return.

Naturally numbers from 0 to 9 were chosen because they are easily mapped to the values of an array with size 10 ([0],...,[9]).

Also the values of the array could be mixed:

String [] numbers = { "five", "one", "three", "four", "two", "six", "seven", "ten", "eight", "nine"};

We don't need them sorted. All we need is a random number between 0 to 9. As long as it is random, we will get randomly one of the elements of the array

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

1/(noOfPhases + 1) = 1/(3+1) = 1/4 = 0 (int)
(int)/(int + int) = int/int = int

1.0/4 = 0.25
double/int = double

Get it?
1/4 = 0
1.0/4 = 0.25

So I would suggest something like this:

double s = 1.0/(noOfPhases + 1);
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

hi

Your point?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
import java.util.*;
public class RadomNumbers {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String [] numbers = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
Random rand = new Random();
int num = rand.nextInt(10)+1;
System.out.println("Generated Random Number between 0 to 10 is : " + numbers[num]);

}
}

Good for you that you gave him the complete solution, when freelancelote and I were trying to help the poster learn. As if we couldn't do what you did. Not to mention that your code is WRONG

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

This is your code:

private JCheckBox [B]team1[/B], team2, team3, team4, team5, team6;

public Selection2(Vector team, Vector [B]team1[/B]) {

}

You define private global team1 JCheckBox variable, AND at the constructor you have:
Vector team1

So when inside the constructor you do this: team1.setEnabled(false); You are referring to the Vector which has no setEnabled method.
Inside the constructor 2 variables exists with the same name:
The team1 argument and the global team1 defined outside the constructor.
Inside the constructor if you want to refer to the CheckBox write: this.team1.setEnabled(false); Although it would be better to rethink the name of all of your variables.
I mean I find this stupid:

private JCheckBox team1;
private Vector Team1 = new Vector();

public Selection2(..., Vector team1) {
Team1 = team1;

Even though it compiles and runs since no rules were violated, later it is very easy to make the mistakes that you made.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
String [] array = {"one", "two", ...} ;
System.out.println(array[[B]0[/B]]);
System.out.println(array[[B]1[/B]]);
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I don't know if this will solve the problem, but at this line:

out.println("<input name='Delete' type='image' src ='images/del.png'value=" + id + " alt='Delete' onclick='javascript<b></b>:return deltest();'/>  ");

You need a space between the value:

'images/del.png'value

And here you need to add the single quotes:

out.println("<input name='Delete' type='image' src ='images/del.png'value='" + id + "' alt='Delete' onclick='javascript<b></b>:return deltest();'/> ");

Like you did for alt='Delete' Also, although the same, you can write it like this:

<input name='Delete' type='image' src ='images/del.png' 
value='<%= id%>' 
alt='Delete' onclick='javascript: return deltest();' />

I think it is easier to write it directly as html inside the jsp file

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Since the range of the random numbers are between 1 and 10, you can create a String array with those numbers:

String [] numbers = {"one", "two", ...};

Then after you get the random number use it as index at the array. You must not forget that the index of the arrays start from 0.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You don't use code tags, nor you specify your problem and at what line you get it. You will get no help this way.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

javaAddict,
if a problem is marked as solved I don't normally look at it anymore. It's helpfull because I can look at other problems instead.
True, perhaps I was being selfish and should have phrase it in another way, so there it goes:
"tejasthacker if you got an answer, please mark the thread as solved".
Better now?

I am sorry then

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
private Vector team1CheckBoxes = new Vector();
....
....

Team1 = team1;
for (int i=0;i<Team1.size();i++) {
  // get the object from the Team1 Vector
  //Team1.get(i);

  JCheckBox box = new JCheckBox( Team1.get(i).toString() );
  box.addActionListener(this)
  
  team1CheckBoxes.add(box);
}

team1CheckBoxes is a Vector that contains JCheckBox objects.
Also you must not forget to display the check boxes. Write another for loop or in the same write code that displays them

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

OK, congrats, you got your code running.

Will you please mark it as solved?
That'll be of great help. Thanks

Why mark it as solved? He didn't solve anything, he just posted the code and asked for help.
And why will it be a great help if it is marked as solved? Why do you want it marked as solved?
How will that help you?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

<servlet-class>: You will put the name of the class. More specifically the name of the Servlet

<url-pattern>: The url that maps to the servlet. Example: If you have link that points to that url, the code inside the servlet will be invoked. You can put that url anywhere you could put the name of a jsp, or any other url

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can have a Vector of JCheckBoxes. Write a for loop that generates them from the Vector team.

Or an array of JCheckBoxes

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

can anyone please make changes and comment my code if u are good at it. that will be really a great help. Thanks again

if u are good at it

Yes, we are good at it

What does this suppose to mean? Have you any idea what you are asking. It's not enough that you cheated at your assignment, you have the nerve to tell us that you cheated and still ask for help. Help not for the code but how to help you cheat better!!

can anyone please make changes and comment my code

It is not your code.

VernonDozier commented: Good points. +24
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I would suggest to continue with the application and implement whatever validation you require as you progress.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I want to draw a line in swing.
Please help me.

Thanks.

Start a new thread. Did you even bother to look the date of this thread? It is 3 years old.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

How do you get the responses?
Are they String objects like the one in your example or are they HttpServletRequest, HttpServletResponse objects?

What you said about the JSP is irrelevant. You don't need a JSP to get the response. If they were, how you think you get the request from a sevlet?

What code do you have to get the response/request?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Then get a book to learn serlvets and jsp. About the database thing there is a post at the top of the jsp forum that describes jsp database connectivity and the MVC model

I don't have the time to perform an online course about servlets in this thread. Nor would be appropriate to write an 100-500 line post with everything I know when you can easily find the same examples in good books.
I cannot explain everything. You must study and then ask specific questions

The post I told you to look is very good in explaining the database part you are interested in

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The first time the page loads this is null:

request.getParameter('searchPhrase')

So this is translated to:

window.open("searchRequestPopUp?searchId=null", . . .

So when the pop up opens the searchId has value "null".

But after you submit the form of page A you send at the request the "searchPhrase" so this has value:

window.open("searchRequestPopUp?searchId=valueEntered", . . .

And you get it at the pop up.

I would suggest to remove this line:

function startPopUp() {
// REMOVE //document.PageAForm.submit();
window.open("searchRequestPopUp?searchId=" +  request.getParameter('searchPhrase') + ",'window','width=800,height=300','scrollbars=yes','resizable=yes','statusbar=yes','menubar=yes','toolbar=yes','dependent=yes');
}

You don't need it. All you need is to open the new page with what you entered at the search text. Right? Then you can do this:

<script type='text/javascript'>
function startPopUp() {

var v = document.getElementById("searchPhraseId").value;

window.open("searchRequestPopUp.jsp?searchId=" +  v, 'window','width=800,height=300','scrollbars=yes','resizable=yes','statusbar=yes','menubar=yes','toolbar=yes','dependent=yes');

}
</script>

<input type="text" name="search" id="searchPhraseId">

<input type='button' value='Search' name='sourceListSearch' onclick='startPopUp()'/>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

thanks dude..i need to know a place where can i learn about those things............

Then look at the java forum, there is a post at the top about beginners in java. After checking that consider which book to read.
Then get a book to learn serlvets and jsp. About the database thing there is a post at the top of the jsp forum that describes jsp database connectivity and the MVC model

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Integer.toString ??
Search somewhere, but I think that it's it.

Actually bernadlosini needs to convert the String from the
"ageTextfield.getText" and the others to int: Integer.parseInt("1.0")

int age = Integer.parseInt( ageTextfield.getText() );
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

the values to array "selElts" are returned from a javascript function

The selElts you define in javascript is not the java array. They are not the same. What you have written doesn't work. You can not call javascript method to give values to java variables.


If you want to send something from one page to the other, you either submit the form and get the values with request.getParameter("")
or set the request Attributes and redirect.

If this doesn't make sense, study harder and learn about servlets and jsp

kvprajapati commented: Good suggeston +13
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Learn java first.
Then search for examples on how java connects to database and executes queries.
Then learn servlets and jsp

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post code of the page that opens the pop-up

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hi,

I'm trying to use the request.getParameter("myInputField") on Page A. The problem is that the input field "myInputField" is on Page B. Does anyone know of a way to retrieve from Page A, the value of the input field that is on Page B?

Thanks,
-Ash

You need to submit the form that is at Page B to the Page A.
In details:
>The input field must be inside a form tag
>When you submit, the control will be transferred to the page that you define at the action attribute of the form tag
> At the next page you will have access to everything the form tag contained:

<form action="pageB.jsp">

<input type="text" name="myInputField" />
<input type="submit" value="Click here to submit" />

</form>

pageB.jsp:

<%
String myInputField = request.gteParameter("myInputField");
%>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Do you know java or html?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I want to create a custom NumberFormat which returns empty string for negative values and the double number iteslf for +ve and zero values. Please guide me....................

Then write a method that:

if value is negative return "empty String"
else return the value

You need to explain your problem better and show some code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You could have this at each page:

<%
        if(session.getAttribute("Username")==null)
            {
response.sendRedirect("mainpage.jsp"); // GO TO LOGIN PAGE
            }
//continue with the rest

This is how I have it. Whenever the user goes to a page, if there is no "session" with the "Username" I redirect to the login page. I do it like this, although I don't believe there is much difference:

RequestDispatcher dispatcher = request.getRequestDispatcher("../index.jsp");
dispatcher.forward(request, response);

You see you cannot prevent the user from just writing the url of the page they want to go at the browser. Anyone can open a browser and write the url of a page that needs login first. But with your code when the page loads, the user is redirected to the login page. I mean once someone hits the 'Back' button they will go back and see what was there. But if the information displayed at that page required some data taken from the request then you will go back.
Example: If you have a form, you submit it and go to a result page. If someone goes 'Back' to that result page they will be asked to resent the data of the request by the browser. When that happens the page reloads and your code will be executed.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Where have you put the code that redirects to the login page and how?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can put an id attribute at the label tag and access it using:
document.getElementById

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post relevant code then

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

If your array is an array of Strings the you can use the compareTo method:
http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)

String s1 = "a";
String s2 = "b";
int i = s1.compareTo(s2);

This method's return values represents which String is greater or lower than the other. So you can use that method for the comparison.

As for the binary search, write a loop that does the following:

>Check if the value to search is in the middle of the array.
>if it's not, check whether it is in the first half or the second half of the array
>After you have found at which half it is, repeat the search, but this time you will not check the entire array but only the half that you found where the value to search is.

remember that the array is already sorted, so the "lower" strings will be at the beginning. That is how you will find which half to choose.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I would suggest to use the split method:

String [] sentenceArray = enteredText.split(" ");
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I have this in my code:

<a href="loginservlet?action=logout">Logout</a>
String action = request.getParameter("action");

And it prints correctly the value "logout".

How do you map the servelt at the web.xml file? Are you sure there is nothing wrong?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You have to be more specific.
Provide code, explain how it behaves, at which cases
and how would you like to behave

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You should add to your pages code that checks the session. If it doesn't contain the user information redirect to the login page.
The user can go directly to whatever page they want by providing url at the browser. Then you need to check if there is a session with user information, else like I said redirect to the login page.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
<a href="ProductController?linkValue=A" target="right">A</a>

And at the servlet use the "request.getParameter"

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you tried using 2 different forms?

<form name="form1" method="post" action="controller/servlet">

  <input type="submit" name="event" value="Add">
</form>

<form name="form2" method="post" action="controller/servlet"
 onsubmit="return confirm("Are you sure?");" >

<input type="submit" name="event" value="Update" >
</form>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Put the code in the main methods inside separate methods and classes and learn jsp

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Have you tried my suggestion? I believe that you can't get the value from a "button", but I might be wrong.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Is your problem that you need to pass the entire object without having to declare different hidden fields?

The only think I can think right now is to put the object directly in the request:

<%
request.setAttribute("USER", simple);
%>

And get it at the next.jsp like this:

<%
bean.UserBean simple =  (bean.UserBean)request.getAttribute("USER");
%>
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What you want, cannot be done. The easiest way is to turn your application into an applet since you will not make much change to the code that renders the GUI

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

What exactly are you trying to accomplish?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Where do you want to be printed?
I would suggest to have a button with each row that has that "onclick" event.
When the rows are dynamically created you can have the value you want to be printed passed as a parameter at a method in the "onclick" event.
I don't if you can just click the row. Check this site to see if the
<tr> tag has an "onclick" event:

Javascript
HTML