DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2006
    Posts
    2

    Calling a function in a class from a web form

    I have a form with a text box and submit button. I have written a java class that takes a string and appends it to a text file. What I want to do it combine these and have a form which action is to call the function in the java class and pass it whatever is in the text box. Below is a simple sketch of what I'm trying to do.

    <Form>
    Username
    Post Action --> UserLog.addUser(Username)
    </Form>

    UserLog.java
    function addUser(String Username)) //Append the username to a file


    How do I tell the form to do whatever is in the java class? I'm very new so a simple and easy answer is appreciated. Thank you.
    Last edited by szahn; 03-03-2006 at 12:10 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Sydney
    Posts
    49

    Servlet/JSP

    Username.jsp

    Code:
    HTML>
    
    <BODY>
    <FORM METHOD=POST ACTION="Username.jsp">
    <INPUT TYPE=TEXT NAME=user SIZE=20>
    <P><INPUT TYPE=SUBMIT></P>
    
    <jsp:useBean id="userLog" class="com.UserLog" scope="request"/>
    
    
    <% 
        String name = request.getParameter("user");
    
        if(name != null && name.length() > 0) {
        	userLog.addUser(name);
        }   
    %>
    
    </FORM>
    </BODY>
    </HTML>

    com.UserLog.java

    Code:
    package com;
    
    public class UserLog {
    
       
        public UserLog() {}
        
        public void addUser(String username) {
            System.out.println(username);
            //add the logic to append the username
        }
    }
    Build a war file which should have

    UserLog.class --> WEB-INF\classes\com folder
    Usename.jsp --> (doc-root)


    The additional files may be required depending on what server you are deploying to

    <app-server>.xml --> WEB-INF

    For example I used JBOSS server to test and had to have

    jboss-web.xml --> WEB-INF folder


    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-web
    	<context-root>test</context-root>
    </jboss-web>

    to test: typed the url http://localhost:8080/test/Username.jsp


    Hope it helps.
    Arul

  3. #3
    Join Date
    Mar 2006
    Posts
    2
    Thanks, this is great help. Will this work without IIS on a standard regular server?

Similar Threads

  1. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  2. Replies: 0
    Last Post: 04-11-2002, 06:48 PM
  3. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 04:21 PM
  4. AnimateWindow API
    By Tim Manos in forum VB Classic
    Replies: 4
    Last Post: 10-19-2001, 06:06 AM
  5. Replies: 2
    Last Post: 06-05-2001, 01:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links