How to configure Eclipse for JBoss Application Server and MySQL Database
To configure Eclipse with JBoss AS and MySQL Database you need Eclipse Indigo for J2EE developer , JBoss Application Server and MySQL Database connector .
You can download those from the following links
1.Eclipse IDE for J2EE Developer : www.eclipse.org/downloads/
2.JBoss http: //www.jboss.org/jbossas/downloads/
3.MySQL database Connctor /J : http://dev.mysql.com/downloads/connector/
4.Tag library jstl.jar from http://www.java2s.com/Code/Jar/JKL/Downloadjstljar.htm
Step 1 .Before you begin, make sure that a JDK package is installed It may already be installed in your Computer if not install it .
Step 2 . Install JBoss as a plugin in Eclipse, you can find many tutorials in this topic . I learnded it from
http://www.packtpub.com/article/jboss-as-plug-in-and-eclipse-web-tools-platform
Step 3. Install Mysql server and Client . This time set your both your username and password to “root”
You can take help from this link : https://help.ubuntu.com/8.04/serverguide/C/mysql.html
Step 4. Copy the MySQL Connector jar file mysql-connector-java-<version>-bin.jar and the Tag Library file jstl.jar file
to the <Jboss server folder>/server/default/lib directory .
Step 5. Start your Jboss server. (You may start it from Eclipse -> Servers Tab-> JBoss -> Start)
Step 6. Now you have to create a Database . To make the MySQL more friendly install MySQL query Browser .
You can install it from Administration → Synaptic Package Manager → mysql-query-manager
Step 7. Creating a Dynamic Web Project
Step 7a. New → Web → Dynamic Web Project and click Next
Step 7b.
- Enter Project Name as TestProject and select select Target Runtime as Jboss v-4.2 and put everything as default and click Next
- Click Next just keep default output folder to default.
- Check the field to generate the web.xml file
- Finish
Add the MySQL-Connector jar file and jstl.jar file into your project .
Copy this two jar file into your Project Folder → WebCOntent->WEB-INF-> lib directory
Right Click on Project Name → Properties → Java build Path and add these 2 jar in the library tag .
Right click on the project name (TestProject) and add new html file , name it as index.html . Keep it now as default .
1.Right click on the project name and add a new servlet to the project
2.name the name servlet TestServlet.java and put in under com.servlet package
3.Put the default urlMapping as /TestServlet
4.Check the following fields as default to give a skeleton of the servlet
5.Finish
[code language=”java”]
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
importjavax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.object.Book;
/**
* Servlet implementation class TestServlet
*/
publicclass TestServlet extends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Connection conn = null;
PrintWriter out = response.getWriter();
Statement stmt = null;
String query;
ResultSet rs;
Book book ;
List list = new ArrayList();
try
{
String userName = “root”;
String password = “root”;
String url = “jdbc:mysql://localhost/test”;
Class.forName (“com.mysql.jdbc.Driver”).newInstance ();
conn = DriverManager.getConnection (url, userName, password);
out.println (“Database connection established”);
}
catch (Exception e)
{
System.err.println (“Cannot connect to database server”);
out.print(“Not connected”);
}
if(conn!=null){
try {
stmt = conn.createStatement();
query=“select * from book”;
rs = stmt.executeQuery(query);
if(rs!=null)
{
while(rs.next()){
book = new Book();
book.setAuther(rs.getString(“auther”));
book.setId(rs.getInt(“id”));
book.setTitle(rs.getString(“title”));
list.add(book);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
request.setAttribute(“book”, list);
if (conn != null)
{
try
{
conn.close ();
out.println (“Database connection terminated”);
}
catch (Exception e) { /* ignore close errors */ }
}
// System.out.print(list.size());
RequestDispatcher view = request.getRequestDispatcher(“/TestJSP.jsp”);
view.forward(request, response);
}
}
[/code]
Adding index.html file
Right on the project and add a html file and name it index.html
Edit it with following :
[code language=”java”]
<!DOCTYPEhtmlPUBLIC“-//W3C//DTD HTML 4.01 Transitional//EN”“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<metahttp-equiv=“Content-Type”content=“text/html; charset=UTF-8″>
<title>Testing Servlet</title>
</head>
<body>
<formaction=“TestServlet”>
<inputtype=“submit”value=“Click Here”>
</form>
</body>
</html>
Adding JSP File
<%@pagelanguage=“java”contentType=“text/html; charset=UTF-8″
pageEncoding=“UTF-8″%>
<!DOCTYPEhtmlPUBLIC“-//W3C//DTD HTML 4.01 Transitional//EN”“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<metahttp-equiv=“Content-Type”content=“text/html; charset=UTF-8″>
<%@tagliburi=“http://java.sun.com/jsp/jstl/core”prefix=“c”%>
<title>Printing from Database</title>
</head>
<body>
Values in the Database :
<c:forEachvar=“l”items=“${book}“>
${l.id}
${l.title}
${l.auther}
<br/>
</c:forEach>
</body>
</html>
[/code]