File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes One Simple Query ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "One Simple Query ??" Watch "One Simple Query ??" New topic
Author

One Simple Query ??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,
I want to send userId and password from my swing Interface to my servlet as just like as we send it from html file,therefore iam sending those parameter to my servlet as query string with the url.For that In swing Interface in the action event of submit button:-
public URL u;
public URLConnection uc;
submit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String userid1;
String password1;
String
url="http://127.0.0.1:8080/examples/servlet/loginServlet";
userid1 = userid.getText();
password1 = password.getText();
u = new URL(url+"?name=" +userid1 + "&path=" + password1);
uc =u.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
//Then what should I do
And In servlet:-
==============
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String str1=req.getParameter("name");
String str2=req.getParameter("path");
System.out.println(str1);
System.out.println(str2);
}
Is it work?Plz Guide me Iam really confused what should I do.
Regards
Bikash
[ January 29, 2003: Message edited by: Bikash Paul ]
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Did you try this out? Is it working or do you face any problems? If you are facing any problems, tell us in particular.
thanks
Sudharsan


Joy is a radiation
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,
yes I already tried with it but it is not connecting with servlet means servlet is not writing its system.out.println statement.Plz help me .Iam a beginner.
Regards
Bikash
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
In debugging servlets you need to try to test the parts separately. Why not make a plain HTML page with a form that does a GET to submit the name and path to the servlet. Use that to get the servlet working, then tackle getting the swing interface working.
Bill
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Hi Bikash,
Try typing the URL you are trying to construct in the browser window and see whats happening. Make sure your URL construction is proper.
sudharsan
[ January 29, 2003: Message edited by: Sudharsan G'rajan ]
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
I could be wrong, but I believe the problem is that the Servlet/Servlet Engine expects any communication to be HTTP-based. Are you sending the proper HTTP commands? I don't see anywhere that you sent a "GET" command. You can't just connect to the URL, you have to send it HTTP commands, just like a browser does.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
That is not needed! If the Servlet Engine gets a request, the default method that will be executed is the 'GET' method. So, the doGet method of the Servlet will be invoked automatically. You can test this with a simple code.
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi all,
Thanks for all of ur's help.I have sloved my problem.Now my swing interface is connecting with my servlet.
Thanks
Bikash
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
If it gets a REQUEST. But NOT if you just open a socket to it. You have to actually SAY something to it. And the only language it speaks is HTTP. Did you try opening a socket to a servlet? See what happens.
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Rob,
Try this code. It opens a connection with the HelloWorldServlet example that comes with Tomcat container. I have not specified any connection type.

Bikash,
How did you solve your problem?
-Sudharsan
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Sudharsan,
I am not completing the cycle thats why my servlet not getting the parameter which iam sending as a query string with the url.Sudharsan can u plz tell me how u post ur above code in small font size & systematic way.Anyway Below r my modified codes:-
submit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{
URL u;
URLConnection uc;
String url="http://127.0.0.1:8080/examples/servlet/loginServlet";
u = new URL(url + "?name=" +URLEncoder.encode(userid.getText()) + "&path=" +URLEncoder.encode(password.getText()));
uc =u.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
BufferedReader inputFromServlet = new BufferedReader(new InputStreamReader(uc.getInputStream()));
....
......
Regards
Bikash
[ January 30, 2003: Message edited by: Bikash Paul ]
Sudharsan Govindarajan
Ranch Hand

Joined: Jul 03, 2002
Posts: 319
Bikash!
When u create a post, you could find see some buttons (like URL, EMAIL, CODE, QUOTE, BOLD, etc.,) below the TextArea for entering the post. If you click on the 'code' button, it will insert the code tags. Enter the code you want to post within those tags.
sudharsan
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi sudharsan,
Thanks for ur help.
Regards
Bikash
[ February 02, 2003: Message edited by: Bikash Paul ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One Simple Query ??