Results 1 to 20 of 67
Thread: get data from html to servlet
- 04-09-2008, 06:31 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
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
- 04-09-2008, 06:37 AM #2
Hi Dear,
use
request.getParameter("password");
request.getParameter("username");
to get the values of these filed in Servlet.
sanjeev
- 04-09-2008, 06:45 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
sanjeev, and how can get the (approval) any data back from servlet to html, so that i can continue opening th emain applicatino page?
- 04-09-2008, 06:46 AM #4
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
and, should i request the data, which is from html, in doGet method?
- 04-09-2008, 06:51 AM #5
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
- 04-09-2008, 06:57 AM #6
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
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'">
- 04-09-2008, 07:11 AM #7
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
- 04-09-2008, 07:34 AM #8
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
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.
- 04-09-2008, 07:39 AM #9
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
want to call the method when the button is selected.. so i dunno what to write on onClick
- 04-09-2008, 07:40 AM #10
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
- 04-09-2008, 07:45 AM #11
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
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.
- 04-09-2008, 07:46 AM #12
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
on application there are different info for different user groups. so i need session data not to be lost
- 04-09-2008, 08:00 AM #13
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
- 04-09-2008, 08:15 AM #14
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
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
- 04-09-2008, 08:17 AM #15
ok lema,
i'll give you the complete sample of this.
sanjeev
- 04-09-2008, 10:38 AM #16
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
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.
- 04-09-2008, 10:49 AM #17
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
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
- 04-09-2008, 10:50 AM #18
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
If you have any queries let me know
- 04-09-2008, 12:22 PM #19
Member
- Join Date
- Apr 2008
- Posts
- 38
- Rep Power
- 0
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.naming:org.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);
}
}
- 04-09-2008, 12:26 PM #20
Similar Threads
-
get data from servlet to html
By lema in forum Java ServletReplies: 7Last Post: 05-22-2008, 04:00 PM -
how to search xml file data based on the given keyword from html form?
By nicemothi in forum XMLReplies: 0Last Post: 04-04-2008, 09:36 AM -
Getting HTML form values in Servlet
By Java Tip in forum Java TipReplies: 0Last Post: 11-17-2007, 08:13 PM -
How to retrieve data from servlet
By valery in forum Java ServletReplies: 1Last Post: 08-06-2007, 08:25 PM -
how to upload a file along with html form data
By pranith in forum Java ServletReplies: 3Last Post: 07-30-2007, 02:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks