Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2008, 07:31 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
get data from html to servlet
hi,

i'm trying to get username (String) and password (String) from html Login page to Servlet, so that i can check the username in db. But i dunno how to use the username in servlet. so, dear experienced guys, pls give a clue.

this is the login page code snippet:

<table>
<tr>
<td>Username: </td>
<td><input name="username" type="text" size="40"></input></td>
</tr>
<tr>
<td>Password: </td>
<td><input name="password" type="password" size="40"></input></td>
</tr>
<tr><td>
<button value="Enter" onClick="location.href='http://111.111.11.111:8080/example/Application.html'">
Enter SMD
</button>
</td></tr>
</table>

thanks in advance, lema
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-09-2008, 07:37 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
Hi Dear,

use

request.getParameter("password");
request.getParameter("username");

to get the values of these filed in Servlet.



sanjeev
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 07:45 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
sanjeev, and how can get the (approval) any data back from servlet to html, so that i can continue opening th emain applicatino page?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 07:46 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
and, should i request the data, which is from html, in doGet method?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-09-2008, 07:51 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
you can do it in doGet or in doPost.

are you using jsp or simple Html pages. If you are using jsp then put your data that you want, into session object and simple get from session object.


sanejev
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-09-2008, 07:57 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
i'm using simple html. but the application is in html with GWT.

so, if i want to send back from servlet a boolean b, how should i get it in html.
..like below..??

<button value="Enter" onClick="location.href=(b=true)?'http://111.111.11.111:8080/example/Application.html':'http://111.111.11.111:8080/example/Wronglogin.html'">
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-09-2008, 08:11 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
why do you want to come back to html page ..??
you can check this condition in servlet and from there you can redirect the control to other pages.

can you provide some more details....if i am wrong.


sanjeev
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-09-2008, 08:34 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
in fact i want to call one method in servlet part, where the username is checked, like below:

public void checkLogin() throws Exception {
LoginRemote loginRemote =(LoginRemote)
findObject("login.server/login/remote");

String[] username = this.getThreadLocalRequest().getParameterValues("u sername");
String[] password = this.getThreadLocalRequest().getParameterValues("p assword");
String u = username[0];
String p = username[0];

SmdUser smdUser = loginRemote.checkLogin(u, p);
// Long res = null;
if (smdUser != null) {
// res = 0L;
smduser = smdUser;
this.getThreadLocalRequest().getSession(true).setA ttribute("user", smdUser);

} else {
// res = 1L;
}
// return res;
}

so, want to open the main Application page if the login is approved, if not then to open a window alert.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-09-2008, 08:39 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
want to call the method when the button is selected.. so i dunno what to write on onClick
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-09-2008, 08:40 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
you can use in servlet

if(b) // if user is valid
RequestDispatcher.forward("give page address here"); // to foward app page
else
RequestDispatcher.forward("give page address here"); // to foward error page
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-09-2008, 08:45 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
oh oh sorry, i want to open the main pagge on the same browser, that's why. because, i tried to open the application on a new window, but it's losen the session data, dunno why. so i thought that i have to open it on the same browser of login page.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-09-2008, 08:46 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
on application there are different info for different user groups. so i need session data not to be lost
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-09-2008, 09:00 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
sorry wrongly i typed.

RequestDispatcher.forward("give page address here"); // to foward app page

correct is

RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/web/html/application.html");
dispatcher.forward(request, response);

and it will open in the same window.


sanjeev
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-09-2008, 09:15 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
sanjeev, but i'm not using doGet, i use only this.getThreadLocalRequest() insrtead of request . i mean where to get request and response for dispatcher.forward(request, response); ? look at the 8th post pls
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-09-2008, 09:17 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
ok lema,

i'll give you the complete sample of this.


sanjeev
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-09-2008, 11:38 AM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
how can i get checkLogin (from 8th post) work by clicking on the button? i typed what you gave in 13th post in the servlet, changed html like this:

<input type="submit" value="Enter SMD"></input>

deployed, and nothing happens by clicking the button. and i don't use doGet nor doPost.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-09-2008, 11:49 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
Hei Lema,
I have a suggetion here. First You make a small design for your small application. As Far as I understood, your requirement seems to
be very easy one. Lets take an mvc approach. Create Two Jsps For User Interaction. One Servlet, One Class For Business Process.

So, login.jsp, welcome.jsp, wrongUser.jsp

LoginServlet,

and LoginAction

Totally You will Have Five Files.

In login.jsp, you collect the user data, and validate using javascript(optional)

make the form action as LoginServlet. In LoginServlet, you just retrieve the data from login.jsp, using request.getParameter(),

In LoginAction Classs You Create Two Methods, dbConnect(), for db connection, and verifyUser(), for userverification.

Write The Code In These methods.

In Login Servlet, just write the logic for redirecting user request. ie if user exists, redirect to welcome.jsp, otherwise, to wrongUser.jsp
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-09-2008, 11:50 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
If you have any queries let me know
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-09-2008, 01:22 PM
Member
 
Join Date: Apr 2008
Posts: 38
lema is on a distinguished road
i change login page to jsp, and it is like this

<form name="LoginServlet" method="post" action="${pageContext.request.contextPath}/LoginServlet.class">

<table>
<tr>
<td>Username: </td>
<td align = "left" width="20"><input name="username" type="text" size="40"></input></td>
</tr>
<tr>
<td>Password: </td>
<td align = "left" width="20"><input name="password" type="password" size="40"></input></td>
</tr>
<tr><td>
<input type="submit" name="enter" value="Enter SMD"></input>
</td></tr>
</table>
</form>

in LoginServlet.java there the code snippet looks like


public class LoginServlet extends RemoteServiceServlet
implements LoginService
{
public LoginServlet() {
super();
}

static public Context getInitialContext() throws Exception
{
java.util.Properties p = new java.util.Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
p.put(Context.URL_PKG_PREFIXES, "jboss.namingrg.jnp.interfaces");
p.put(Context.PROVIDER_URL, "localhost:1099");
return new javax.naming.InitialContext(p);
}

private static Object findObject(String name) {
try {
Context ctx = getInitialContext();
return ctx.lookup(name);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public void checkLogin(String u, String p) throws Exception {
LoginRemote loginRemote =(LoginRemote)
findObject("example.server/login/remote");



SmdUser smdUser = loginRemote.checkLogin(u, p);
if (smdUser != null) {
this.getThreadLocalRequest().getSession(true).setA ttribute("user", smdUser);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("http://111.111.11.111:8080/example/Application.html");
dispatcher.forward(getThreadLocalRequest(), getThreadLocalResponse());

} else {
Window.alert("You are not SMD member!");

}

}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
processRequest(request, response);
}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String actionName = "";
String pathInfo = request.getPathInfo();
System.out.println("PATH INFO IS: " + pathInfo);
String destinationPage = "/smderror.jsp";

if (pathInfo != null) {
actionName = pathInfo.substring(1); // Get the action name from the HTTP Request.
System.out.println("ACTION NAME IS: " + actionName);
}


// perform action
if("loginServlet".equals(actionName))
{
String[] username = request.getParameterValues("username");
String[] password = request.getParameterValues("password");
String u = username[0];
String p = username[0];
try {
checkLogin(u, p);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
String errorMessage = "[" + actionName + "] is not a valid action.";
request.setAttribute("/smderror.jsp", errorMessage);
}


// Redirect to destination page.
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destinati onPage);

dispatcher.forward(request, response);
}

}
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 04-09-2008, 01:26 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
Is this working fine or Not?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
get data from servlet to html lema Java Servlet 7 05-22-2008 05:00 PM
how to search xml file data based on the given keyword from html form? nicemothi XML 0 04-04-2008 10:36 AM
Getting HTML form values in Servlet Java Tip Java Tips 0 11-17-2007 09:13 PM
How to retrieve data from servlet valery Java Servlet 1 08-06-2007 09:25 PM
how to upload a file along with html form data pranith Java Servlet 3 07-30-2007 03:33 AM


All times are GMT +3. The time now is 01:33 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org