Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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.
[676 byte] By [szahn] at [2007-11-11 8:08:30]
# 1 Re: Calling a function in a class from a web form
Username.jsp

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

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

<?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 at 2007-11-11 22:36:18 >
# 2 Re: Calling a function in a class from a web form
Thanks, this is great help. Will this work without IIS on a standard regular server?
szahn at 2007-11-11 22:37:23 >