Connecting an applet to a JSP.
- First of all, create a Java Application project with your applet
in it
- The submit button should read the data from the screen, encode
them into a URL, and then initiate the web page using the
"showDocument()" method
- Note, any special characters in the data need to be converted,
use the URLEncoder to do this
- In the example below, a jsp file named JspForm.jsp (located in
the same folder as the jar file on the web server) is invoked with the
parameters lastname, firstname, age, and haircolor. Note the "?"
separating the jsp file from the paramters and the "&" separating
the parameters from one another. Care should be taken to make sure that
special characters are not embedded in the data being passed (that
is, no "=", "&", or "?" as part of the names). The
output of the JSP is directed to a window called "OutputWindow" (this
needs to be created in the HTML that has the applet tag), optionally
the target could be "_blank", "_self", etc.
private void
submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling
code here:
String last = lastField.getText(); //
get data from screen
String first = firstField.getText();
String age = ageField.getText();
String hair =
(String)hairCombo.getSelectedItem();
//
// fix any spaces or special characters
in the data
//
try{
last = URLEncoder.encode(last, "UTF-8");
first = URLEncoder.encode(first, "UTF-8");
age = URLEncoder.encode(age, "UTF-8");
hair = URLEncoder.encode(hair, "UTF-8");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,ex.toString());
return;
}
try{
//
create a url with the data encoded on it
URL link = new
URL(getCodeBase()+"JspForm.jsp?"+
"lastname="+last+ "&"+
"firstname="+first+ "&"+
"age="+age+"&"+
"haircolor="+hair);
getAppletContext().showDocument(link,"OutputWindow"); // Fire up the
jsp and tell it where to send its output
}
catch(MalformedURLException ex) {
JOptionPane.showMessageDialog(this, "Bad URL: JspForm.jsp");
}
}
JSP Reception of the parameters
- Your JSP will have code the calls the getParameter method of the
request object for each paramter. Be sure to spell the parameter names
the same way in the applet ad in the JSP.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP
Page</title>
</head>
<body>
<h3>Your Information
:</h3>
<%
String last = request.getParameter("lastname");
String first = request.getParameter("firstname");
String age = request.getParameter("age");
String haircolor = request.getParameter("haircolor");
%>
<b>Name : <%= first+" "+last %>
</b><br/>
<b>Age : <%= age %></b>
<br/>
<b>Hair Color : <%= haircolor %>
</b> <br/>
</body>
</html>
Web page to contain both
- In the example below, the applet is on the page and in
IFrame is used to create a place for the output. The use of an
IFrame is optional.
<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP
Page</title>
</head>
<body>
<h1>Applet to JSP
Connection Demo</h1>
<Applet
archive="AppletProject.jar" code="AppletForm.class" width="300"
Height="260"></applet>
<IFrame
name="OutputWindow" width="300" height="300">
</body>
</html>
Click here to download the NetBeans Project