Basically you're asking something like: How to deploy a java applet for today's browsers (applet, embed, object)?
Based on that, I think what you want is:
<object
classid="clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA"
style="height: 500px; width: 700px;">
<param name="code" value="FinalProject.class">
<comment>
<embed code="FinalProject.class"
type="application/x-java-applet"
height="500" width="700">
<noembed>
This browser appears to lack support for Java Applets.
</noembed>
</embed>
</comment>
</object>
Now, you have a filename of main.FinalProject.class in your code. It seems like FinalProject.class would be more likely. But yours could be right. In any case, this html file needs to be in the same folder as main.FinalProject.class or FinalProject.class and whatever classes may also be required.
Now, you may also need to make sure your browsers can actually run an applet. See: How do I enable Java in my web browser?
Update
Based on feedback from Andrew Thompson, the preferred solution is to use JavaScript from Oracle, like this:
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {
code:'FinalProject.class',
width:700, height:500} ;
var parameters = {}; // does the Applet take parameters?
var version = '1.6' ; // does the Applet require a minimum version of Java
deployJava.runApplet(attributes, parameters, version);
</script>
This requires the ability to load arbitrary JavaScript, but you could also capture that deployJava.js and have that be local as well. Might be worth a look.
Answer from artlung on Stack OverflowLinking files to html and java script
Best way to embed java code into an html file? - Stack Overflow
html - How do I embed a java program into my website? - Stack Overflow
Run any Java in HTML pages with one line of <script>
Videos
You cannot. The browsers only understand javascript. Java is used as a server side language for server side applications inside an application server like Tomcat or JBoss (if you use Spring Boot you can use an embedded Tomcat), the same (but more powerful in my opinion) way you need an Apache server with a PHP interpreter to serve php pages.
You can use a JSP page. But they are intended for generating HTML in the server side. When you load the HTML in the browser the Java code has ended its execution and you cannot do any interaction with the Java side other than reload the same or other page entirely or using ajax requests. In any case, the Java code is executed in a server, not in the browser.
You don't need to create a .jsp file even that it's outdated. You can add a <script> …</script>like this:
<script language=JavaScript>
<!--type the javascript code here-->
</script>
I've created a simple JavaScript file that lets you turn any element in an HTML page into an embedded Java editor/runner with one line of JS code. You simply add this call in a <script> tag:
SnapCode.addPlayButtonToElementForId(myId);
This adds a 'play' button to the named element, and when clicked it takes all inner text and opens it in a SnapCode frame and runs it as Java REPL. Here's an example of a simple Java tutorial page that has been made fully live Java with a couple lines of <script> code:
Here's a sample link: https://reportmill.com/shared/learn_java.html
There are a ton of really cool things about it:
It runs entirely in the browser client (no sever needed)
It supports console input, graphics, animation, UI and even Swing
It allows full editing with code-complete, error checking, etc.
It can take you to the full SnapCode IDE
[Edit:] Here is a link to instructions how to do this.
If you want to do that yourself, without using any external library, a clean way would be to create a template.html file with all the static content, like for example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>$title</title>
</head>
<body>$body
</body>
</html>
Put a tag like $tag for any dynamic content and then do something like this:
File htmlTemplateFile = new File("path/template.html");
String htmlString = FileUtils.readFileToString(htmlTemplateFile);
String title = "New Page";
String body = "This is Body";
htmlString = htmlString.replace("$title", title);
htmlString = htmlString.replace("$body", body);
File newHtmlFile = new File("path/new.html");
FileUtils.writeStringToFile(newHtmlFile, htmlString);
Note: I used org.apache.commons.io.FileUtils for simplicity.
A few months ago I had the same problem and every library I found provides too much functionality and complexity for my final goal. So I end up developing my own library - HtmlFlow - that provides a very simple and intuitive API that allows me to write HTML in a fluent style. Check it here: https://github.com/fmcarvalho/HtmlFlow (it also supports dynamic binding to HTML elements)
Here is an example of binding the properties of a Task object into HTML elements. Consider a Task Java class with three properties: Title, Description and a Priority and then we can produce an HTML document for a Task object in the following way:
import htmlflow.HtmlView;
import model.Priority;
import model.Task;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class App {
private static HtmlView<Task> taskDetailsView(){
HtmlView<Task> taskView = new HtmlView<>();
taskView
.head()
.title("Task Details")
.linkCss("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css");
taskView
.body().classAttr("container")
.heading(1, "Task Details")
.hr()
.div()
.text("Title: ").text(Task::getTitle)
.br()
.text("Description: ").text(Task::getDescription)
.br()
.text("Priority: ").text(Task::getPriority);
return taskView;
}
public static void main(String [] args) throws IOException{
HtmlView<Task> taskView = taskDetailsView();
Task task = new Task("Special dinner", "Have dinner with someone!", Priority.Normal);
try(PrintStream out = new PrintStream(new FileOutputStream("Task.html"))){
taskView.setPrintStream(out).write(task);
Desktop.getDesktop().browse(URI.create("Task.html"));
}
}
}
I won't go by your code. But the example here will be enough.
The standard way of passing/submitting data to the server in the pure Servlets/JSP world is by using HTML form, i.e. the same way as when use other server side languages for example php. And it doesn't matter whether it is a pure HTML page or JSP page. The recommended/most used method of submitting data from the form to the server is POST or GET.
Its standard way to submit data using POST method and respectively to process the submitted data using the doPost() method in your servlet.
for example:
<form name="something" method="post" action="<servlet-name>"> //if u want to change the action to something else then u need to modify your xml file.
<input type="text" name="username"/>
<input type="submit" name="submitit" value="submited"/>
</form>
now in the servlet under the doPost(...) write
if(request.getParameter("submitit").equals("submitted")){
String username=request.getParameter("username");
//now u can run a query and insert ito to database;
}
in the end you can redirect it another page with
`response.sendRedirect();`
or any other way may i assume that you are using eclipse Java EE ide for development. then u need not worry about integrating them, eclipse would prepare the xml files for you once you create a new Java EE project. and if not then u have to do it manually, i once tried to do it, but I couldn't succeed. here is a link: that would interset you, i hope: http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
this is bad thing, but i will edit the code for you. by the way, i am removing the javascript. KISS (keeping it simple silly).. :) ur jsp page would be:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>FORM</title>
</head>
<body>
<form action="test1" method="post" >
<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="name" maxlength="10"></td>
</tr>
<tr>
<td>roll:</td>
<td><input type="text" name="rollno" maxlength="5"></td>
</tr>
<tr>
<td>class:</td>
<td><input type="text" name="class" maxlength="10"></td>
</tr>
<tr>
<td>Mobile:</td>
<td><input type="text" name="mobileno" maxlength="10"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
and the servlet will be:
import java.sql.*;
class test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection con = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.106.87:1521:ORA11G","fuel_db","foel");
String name = request.getParameter("name");
String roll = document.getElementById("rollno");// idk why roll no is string
String clas_s = document.getElementById("class");
String mobile = document.getElementById("mobileno");
try {
String query= "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?);";
pstmt = con.prepareStatement(query);
pstmt.setString(1,name);
pstmt.setString(2,roll);
pstmt.setString(3,clas_s);
pstmt.setString(4,mobile);
pstmt.executeUpdate();
con.close();
}
catch(Exception e) {
e.printStackTrace();}
response.sendRedirect("confirm.jsp");
}
}
donot ask me about the braces.. fix it yourself.
First create some method like submitData(data) in your Java class.
public class test1 {
public void submitData(String name,String rollno,String classData,String mobileno) throws SQLException, ClassNotFoundException {
Connection con = null;
try {
Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.106.87:1521:ORA11G","fuel_db","foel");
Statement statement = con.createStatement();
String command = "INSERT INTO student (name, rollno, class, mobileno) VALUES (" + name + "," + rollno + "," + classData + "," + mobileno + ");";
statement.executeUpdate(command);
con.close();
}
}
In your HTMl page index.html or index.jsp, you need to put a form and then post it to JSP page which has the logic I have mentioned below.
<FORM NAME="form1" ACTION="ProcessData.jsp" METHOD="POST">
In your JSP page you may get data when a form is submitted using POST method. Get all those variables using request.getParameter("name")
Then in your JSP put that java code in <% %> blocks inside body tag. Remember JSP is Java in HTML! In your ProcessData.jsp
<%
String name = request.getParameter("name");
//add null checks and all
//Similarly get all datamobileno etc
//then call your submitData() method
test1 myTest = new test1();
myTest.submitData(....)
%>
Also take care of naming conventions.
In java
- Classes name start with Caps. So class name must be Test1 and not test1.
- Functions, variables must be in Camel case like myTest.
Hey everyone! I am a beginner programmer (started 1,5 months ago) and I just followed some tutorials and managed to create the front end of the login and registration of the user, using html, css and JS (with VS Code). I was also able, with some more tutorials and intense research, to create the back end of a page with login and registration using Java (with IntelliJ) and it also works. But I can not find a way to link/connect these 2 files in order for the website to work. I have watched many videos but none seem to fit my case (prob because of my lack of knowledge). I have found a tutorial that might work, but I would need to convert all my html css and JS to JSX. Can someone please help me with what/how is the best way to do this? Ps.: I know it is very soon for me to want something like this, but I really need it.