Posted on February 19th, 2010
5467 views Hello to everyone. Are you a JAVA coder or do you like JAVA? Would you like to do great websites with your already known JAVA language? Well let me give you some guidance on how to do it. For website development in JAVA language we have JSP (Java Server Pages). JSPs are documents with ".jsp" extension that have normal HTML/XHTML/CSS/JS etc.. but they can have incrustrated JAVA code, just like php. This is not a tutorial for beginners in JAVA so I expect you to know atleast how to code in JAVA and HTML and how does ".jsp" files work. If you don't know about this you can always read the tutorial but maybe you'll need to read other sources to understand it all. Requirements:
Note: This tutorial uses Microsoft SQL server, but you can use mysql if you change the connection class. It seems that the CODE-BOX isnt working well so Im attaching a zip file with all the code in it. I attached it but I can't insert it here =/. Result: The result will be a Todo-List web application that connects to a MSSQL Server database using the MVC arquitectural pattern. This patter will be applied using:
� STEP 1 (Creating front-page) � To begin the actual tutorial the firt part will be a simple jsp file. To do this I hope you have Eclipse open and you've created a new Dinamic Web Project. As I said this is not for beginners so if you dont know how to create a Dinamic Web Project or you dont know how to use Eclipse, this tutorial isn't for you. As I said earlier create a new jsp file in the "WebContent" folder named: "index.jsp". As you already know HTML I will not explain this part as its only HTML for now.
<%@ 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"> <%@page import="models.Item"%><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Todo-List App</title> </head> <body> <h2>Add a new item</h2> <form action="" method="post"> <table> <tr> <td><input type="text" value="I have to..." onClick="this.value('')" name="todo"/></td> <td><input type="submit" value="Send" name="send"/> </td> </tr> </table> </form> <h2>Todo-List</h2> </body> </html> That code will only be the skeleton where we'll later put some incrustated Java code for it to work as we want it to. STEP 2 (Creating result page) As a second Step we will create another jsp file that will be our result page where we'll direct the user once we manage the data in the Servlet. Create another jsp file in "WebContent" folder named "result.jsp" and add this code: � <%@ 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>Insert title here</title> </head> <body> <!-- This will show if insertion in db was made successfully --> <h2>Item added to Todo-List</h2> <!-- This will show if insertion in db was not made successfully --> <h2>There was a problem and the item wasn't added. Please try again</h2> <!-- This will always show --> To return to the main page click <a href="index.jsp">Here!</a> </body> </html> � Now we have all our html ready. The only part left is the servlet. But this tutorial is also about Java so I will like to make a simple SQLConnection class and maybe a Item model class. So chech it out in the next page.
|