Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

divide jsp file into html and jsp

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
Ronayne
Asked:
Ronayne
  • 11
  • 9
1 Solution
 
vikram_1982Commented:
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
 
jarasaCommented:
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
 
RonayneAuthor Commented:

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jarasaCommented:
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
 
RonayneAuthor Commented:

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
 
jarasaCommented:
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
 
RonayneAuthor Commented:

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
 
RonayneAuthor Commented:

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
 
jarasaCommented:
Well but what you want to do then, turn back to the previous page?? or what??
Javier
0
 
RonayneAuthor Commented:

Just stay on the same page and clear the text boxes.
0
 
jarasaCommented:
OK

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

Javier
0
 
RonayneAuthor Commented:

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
 
RonayneAuthor Commented:

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
 
RonayneAuthor Commented:

I've just doubled the points, I need this to work
0
 
jarasaCommented:
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
 
RonayneAuthor Commented:

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
 
jarasaCommented:
Did you executed the code as I posted??
0
 
RonayneAuthor Commented:

<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
 
jarasaCommented:


<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
 
RonayneAuthor Commented:

Thank you.
0
 
jarasaCommented:
My pleasure.
Javier
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now