Page 1 of 4 123 ... LastLast
Results 1 to 20 of 67
  1. #1
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default 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

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hi Dear,

    use

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

    to get the values of these filed in Servlet.



    sanjeev

  3. #3
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    sanjeev, and how can get the (approval) any data back from servlet to html, so that i can continue opening th emain applicatino page?

  4. #4
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    and, should i request the data, which is from html, in doGet method?

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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

  6. #6
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    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'">

  7. #7
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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

  8. #8
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  9. #9
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    want to call the method when the button is selected.. so i dunno what to write on onClick

  10. #10
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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

  11. #11
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  12. #12
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    on application there are different info for different user groups. so i need session data not to be lost

  13. #13
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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

  14. #14
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    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

  15. #15
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    ok lema,

    i'll give you the complete sample of this.


    sanjeev

  16. #16
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  17. #17
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    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

  18. #18
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    If you have any queries let me know

  19. #19
    lema is offline Member
    Join Date
    Apr 2008
    Posts
    38
    Rep Power
    0

    Default

    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);
    }

    }

  20. #20
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

Page 1 of 4 123 ... LastLast

Similar Threads

  1. get data from servlet to html
    By lema in forum Java Servlet
    Replies: 7
    Last Post: 05-22-2008, 05:00 PM
  2. Replies: 0
    Last Post: 04-04-2008, 10:36 AM
  3. Getting HTML form values in Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-17-2007, 09:13 PM
  4. How to retrieve data from servlet
    By valery in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2007, 09:25 PM
  5. how to upload a file along with html form data
    By pranith in forum Java Servlet
    Replies: 3
    Last Post: 07-30-2007, 03:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •