Solved

divide jsp file into html and jsp

Posted on 2004-03-29
21
337 Views
Last Modified: 2010-04-01
Hi, below is a jsp file, how can i divide this jsp into a html and jsp file, ie the html is in one file and the jsp is another file that accesses the html form elements. Cheers

<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="classfiles.*" %>
<%@ page import= "java.io.*" %>
<%@ page import= "java.net.*" %>
<%@ page import= "java.security.*" %>
<%@ page import= "java.util.Date" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>

<HEAD><TITLE>Place Order</TITLE></HEAD>
<HTML>

<BODY>
<center><h1>Enter Item Details</h1></center>
<br>
<br>

<center><br><br>
<table border="2" cellpadding="5" cellspacing="2">

<tr>
<form name="Form3" method="POST" action="makexml.jsp">
<td><font size = 5>Enter <b>Name</b>:  </td>
<td><input type=text name="pname"><br></td>
</tr>

<tr><td><font size = 5>Enter <b>Type</b>:  </td>
<td><input type=text name="ptype"><br></td>
</tr>
<tr><td><font size = 5>Enter <b>Amount</b>:  </td>
<td><input type=text name="pamt"><br></td>
</tr>

<tr><tr><tr>
<tr><td><font size = 6><input type=submit value="        Place Order         " >
</table>
</center>
</body>
</html>

<%
 String inServer = "localhost";
 String inDBName = "test";
 String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;
 
// Identify a carriage return character for each output line
int iLf = 10;
char cLf = (char)iLf;

File outputFile = new File("c:\\seanproj\\xmlfile.xml");
outputFile.createNewFile();
FileWriter outfile = new FileWriter(outputFile);
try {
 
  String pnam = request.getParameter("pname");
  String ptyp = request.getParameter("ptype");
  String pam = request.getParameter("pamt");
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  Connection con = DriverManager.getConnection(DATABASE_URL, "username", "password");
  Statement stmt1 = con.createStatement();
  int rs1 = stmt1.executeUpdate("INSERT INTO products (name, type, amt) VALUES (\""+pnam+"\", \""+ptyp+"\" , \""+pam+"\")");
  Statement stmt = con.createStatement();
                 
  Statement stat = con.createStatement();
 
   outfile.write("<?xml version='1.0' encoding='ISO-8859-1'?>"+cLf);
   outfile.write("<?xml-stylesheet type='text/xsl' href='orderstyle.xsl'?>"+cLf);
  outfile.write("<Table>"+cLf);

   outfile.write("  <Product>"+cLf);
   outfile.write("    <Name>" +pnam  +"</Name>"+cLf);
   outfile.write("    <Type>" +ptyp +"</Type>"+cLf);
   outfile.write("    <Amount>" +pam +"</Amount>"+cLf);
   outfile.write("  </Product>"+cLf);

 outfile.write("</Table>"+cLf);
 
 stat.close();
 outfile.close();
}
 catch (ClassNotFoundException cnfe) {
         out.println(cnfe.toString());
       }
       catch (SQLException sqle) {
         out.println(sqle.toString());
 
}
   
catch( Exception er ) {
%>
 <exception><%= er.getMessage()%></exception>
<%
 outfile.close();
}                                                        
%>


0
Comment
Question by:Ronayne
  • 11
  • 9
21 Comments
 
LVL 2

Expert Comment

by:vikram_1982
ID: 10711454
U may want to consider using JSP Beans.. They have been created especially for this purpose. Other than that i am not sure if it can be done, as JSP requires interleaved JSP and HTML code to make sense.. Beans would be ur best choice.
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10711854
Hi ronayne.

I really don't get what you want to do, as vikram says you can write HTML code inside a JSP, you don't need to put the outfile.write method, not even out.println just the HTML Code and <%= varName %> for the results like this:

<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type='text/xsl' href='orderstyle.xsl'?>
<Table>
<Product>
<Name> <%=pnam %>  </Name>
<Type> <%=ptyp %></Type>
<Amount> <%=pam %></Amount>
</Product>
</Table>

But as I see you're writting XML not HTML it will work either.

Javier

 
0
 

Author Comment

by:Ronayne
ID: 10712201

i just want to wrap javascript confirmation around the file, so the user is aked if he/she wants to create the file or not
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10712225
And what would happens if he/she does not want to generate the file?

Anyway do this:

<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="classfiles.*" %>
<%@ page import= "java.io.*" %>
<%@ page import= "java.net.*" %>
<%@ page import= "java.security.*" %>
<%@ page import= "java.util.Date" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>

<HEAD>
<TITLE>Place Order</TITLE>
<Script>
function sendForm() {
if( confirm("Do you wish to generate the XML File?"))
    Form3.submit();
else
    //Do what you want to do here
}
</Script>
</HEAD>
<HTML>

<BODY>
<center><h1>Enter Item Details</h1></center>
<br>
<br>

<center><br><br>
<table border="2" cellpadding="5" cellspacing="2">

<tr>
<form name="Form3" method="POST" action="makexml.jsp">
<td><font size = 5>Enter <b>Name</b>:  </td>
<td><input type=text name="pname"><br></td>
</tr>

<tr><td><font size = 5>Enter <b>Type</b>:  </td>
<td><input type=text name="ptype"><br></td>
</tr>
<tr><td><font size = 5>Enter <b>Amount</b>:  </td>
<td><input type=text name="pamt"><br></td>
</tr>

<tr><tr><tr>
<tr><td><font size = 6><input type=button value="        Place Order         "  onClick="sendForm()">
</table>
</center>
</body>
</html>

<%
 String inServer = "localhost";
 String inDBName = "test";
 String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;
 
// Identify a carriage return character for each output line
int iLf = 10;
char cLf = (char)iLf;

File outputFile = new File("c:\\seanproj\\xmlfile.xml");
outputFile.createNewFile();
FileWriter outfile = new FileWriter(outputFile);
try {
 
  String pnam = request.getParameter("pname");
  String ptyp = request.getParameter("ptype");
  String pam = request.getParameter("pamt");
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  Connection con = DriverManager.getConnection(DATABASE_URL, "username", "password");
  Statement stmt1 = con.createStatement();
  int rs1 = stmt1.executeUpdate("INSERT INTO products (name, type, amt) VALUES (\""+pnam+"\", \""+ptyp+"\" , \""+pam+"\")");
  Statement stmt = con.createStatement();
               
  Statement stat = con.createStatement();
 
   outfile.write("<?xml version='1.0' encoding='ISO-8859-1'?>"+cLf);
   outfile.write("<?xml-stylesheet type='text/xsl' href='orderstyle.xsl'?>"+cLf);
  outfile.write("<Table>"+cLf);

   outfile.write("  <Product>"+cLf);
   outfile.write("    <Name>" +pnam  +"</Name>"+cLf);
   outfile.write("    <Type>" +ptyp +"</Type>"+cLf);
   outfile.write("    <Amount>" +pam +"</Amount>"+cLf);
   outfile.write("  </Product>"+cLf);

 outfile.write("</Table>"+cLf);
 
 stat.close();
 outfile.close();
}
 catch (ClassNotFoundException cnfe) {
         out.println(cnfe.toString());
       }
       catch (SQLException sqle) {
         out.println(sqle.toString());
 
}
   
catch( Exception er ) {
%>
 <exception><%= er.getMessage()%></exception>
<%
 outfile.close();
}                                                        
%>

Javier

0
 

Author Comment

by:Ronayne
ID: 10712700

well if he/she does'nt want to generate the file the cancel button will be hit, I dont think I know what you mean
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10713186
Well if he/she does not want to generate the file why should he/she click on the Button?

I mean that maybe you want to do other thing like showing the values of the data on the same page or redirect to the main page or whatever.

Did what I told you worked anyway?

Javier
0
 

Author Comment

by:Ronayne
ID: 10716090

Actually I cant try it becaue i wont have access to that machine for a a day or two. Its just a confirmation that the user want to create the file, he/she may click the button by mistake, thats it
0
 

Author Comment

by:Ronayne
ID: 10751813

Your code works but its not fully doing what I wanted.

<Script>
function sendForm() {
if( confirm("Place this Order?"))
{
    Form3.submit();
    alert("Order has been Sent");
}
else
    //cancel making xml file
}
</Script>

I just want to amend this so that if the cancel button is hit the xmlfile will not be made.
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10756071
Well but what you want to do then, turn back to the previous page?? or what??
Javier
0
 

Author Comment

by:Ronayne
ID: 10760602

Just stay on the same page and clear the text boxes.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:jarasa
ID: 10763718
OK

<Script>
function sendForm() {
if( confirm("Place this Order?"))
{
    Form3.submit();
    alert("Order has been Sent");
}
else
    document.Form3.refresh();
}
</Script>

Javier
0
 

Author Comment

by:Ronayne
ID: 10768526

Cheers, just one more thing, how can I also re - direct the user to a different page one the ok button is clicked in the script?
0
 

Author Comment

by:Ronayne
ID: 10768550

Ive just noticed something, when the cancel button is hit the xml file is still generated, i would like the xml file not to be generated when the cancel button is hit, maybe a bean would be the best option?
0
 

Author Comment

by:Ronayne
ID: 10770120

I've just doubled the points, I need this to work
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10772858
The problem is that you're generating the xml File before the user does anything, remember that JSP is server side when the user calls you jsp it generates the xml and the show the user the form, if you don't wnat to do that just take off the xml part and put it on a different jsp wich will be called on the action of the form.

Something like this:

JSP 1 --> placeOrder.jsp or html if you like it does not matter, you don't need Java code here

<HEAD><TITLE>Place Order</TITLE></HEAD>
<Script>
function sendForm() {
if( confirm("Place this Order?"))
{
    Form3.submit();
    alert("Order has been Sent");
}
else
    document.Form3.refresh();
}
</Script>
<HTML>

<BODY>
<center><h1>Enter Item Details</h1></center>
<br>
<br>

<center><br><br>
<table border="2" cellpadding="5" cellspacing="2">

<tr>
<form name="Form3" method="POST" action="makexml.jsp">
<td><font size = 5>Enter <b>Name</b>:  </td>
<td><input type=text name="pname"><br></td>
</tr>

<tr><td><font size = 5>Enter <b>Type</b>:  </td>
<td><input type=text name="ptype"><br></td>
</tr>
<tr><td><font size = 5>Enter <b>Amount</b>:  </td>
<td><input type=text name="pamt"><br></td>
</tr>

<tr><tr><tr>
<tr><td><font size = 6><input type=button value="        Place Order         " onClick=sendForm();>
</table>
</center>
</body>
</html>



JSP 2 --> makexml.jsp generate the File


<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="classfiles.*" %>
<%@ page import= "java.io.*" %>
<%@ page import= "java.net.*" %>
<%@ page import= "java.security.*" %>
<%@ page import= "java.util.Date" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page contentType="text/plain" %>

<%
 String inServer = "localhost";
 String inDBName = "test";
 String DATABASE_URL = "jdbc:mysql://" + inServer + ":3306/" + inDBName;
 
// Identify a carriage return character for each output line
int iLf = 10;
char cLf = (char)iLf;

File outputFile = new File("c:\\seanproj\\xmlfile.xml");
outputFile.createNewFile();
FileWriter outfile = new FileWriter(outputFile);
try {
 
  String pnam = request.getParameter("pname");
  String ptyp = request.getParameter("ptype");
  String pam = request.getParameter("pamt");
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  Connection con = DriverManager.getConnection(DATABASE_URL, "username", "password");
  Statement stmt1 = con.createStatement();
  int rs1 = stmt1.executeUpdate("INSERT INTO products (name, type, amt) VALUES (\""+pnam+"\", \""+ptyp+"\" , \""+pam+"\")");
  Statement stmt = con.createStatement();
               
  Statement stat = con.createStatement();
 
   outfile.write("<?xml version='1.0' encoding='ISO-8859-1'?>"+cLf);
   outfile.write("<?xml-stylesheet type='text/xsl' href='orderstyle.xsl'?>"+cLf);
  outfile.write("<Table>"+cLf);

   outfile.write("  <Product>"+cLf);
   outfile.write("    <Name>" +pnam  +"</Name>"+cLf);
   outfile.write("    <Type>" +ptyp +"</Type>"+cLf);
   outfile.write("    <Amount>" +pam +"</Amount>"+cLf);
   outfile.write("  </Product>"+cLf);

 outfile.write("</Table>"+cLf);
 
 stat.close();
 outfile.close();
}
 catch (ClassNotFoundException cnfe) {
         out.println(cnfe.toString());
       }
       catch (SQLException sqle) {
         out.println(sqle.toString());
 
}
   
catch( Exception er ) {
%>
 <exception><%= er.getMessage()%></exception>
<%
 outfile.close();
}                                                        
%>

Javier

>I've just doubled the points, I need this to work
Forget about it
:c)

0
 

Author Comment

by:Ronayne
ID: 10775230

I tried your code but when the button clicked and the javascript alret is displayed, regardless of the option checked (ok/cancel) none of the java script commands get executed.
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10775281
Did you executed the code as I posted??
0
 

Author Comment

by:Ronayne
ID: 10775286

<Script>
function sendForm() {
if( confirm("Place this Order?"))
{
    document.Form3.submit();
    alert("Order has been Sent");
}
else
    document.Form3.refresh();

}
</Script>

This was missing in your original script :  document.Form3.submit();.
 The xml file now gets made but when cancel is hit the form fields remain full, any ideas?
Cheers
0
 
LVL 6

Accepted Solution

by:
jarasa earned 100 total points
ID: 10775321


<Script>
function sendForm() {
if( confirm("Place this Order?"))
{
    document.Form3.submit();
    alert("Order has been Sent");
} else {
    document.Form3.pname.value="";
    document.Form3.ptype.value="";
    document.Form3.pamt.value="";
}
}
</Script>

0
 

Author Comment

by:Ronayne
ID: 10775362

Thank you.
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10775397
My pleasure.
Javier
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jAVA SERVLETS CODE FOR ONLINE SUPERMARKE 2 196
Run the project 5 87
dateFashion java challenge 9 132
Limiting the number of characters a user can type in a text field 6 51
What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Fine Tune your automatic Updates for Ubuntu / Debian
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now