1) Install Netbeans 6.9 or later
2) Download GlassFish from this site
install GlassFish, it will run easy
3) Run NetBeans IDE and view Services window
Right Click on servers the click on "add server.."
Type instead of x the version of the GlassFish you want to add
the press Next and type the link of the GlassFish installation folder
press Next and Select the domain and press Finish
** Now your GlassFish Created
4) Now you have to start the server by right click on GlassFish 3.1 and click start
if the server didn't start the solution is bellow
5) Creating GlassFish Domain
open your glass fish directory and open bin folder
double click on "asadmin" it will run and type "domain" then an error list will shown like the figure bellow
never mind, just I want to show you how you can get correct commands
now type "create-domain domain2" and wait for response
back to netbeans and repeat step 3 but choose domain2 and run the server
---------------------------------------------
Now we want to configure MSSQL server
1) install SQL server 2005 or later
2) Create your DB with windows authentications
3) Create new user with Sql authentications with password and select the DB to be shown to this user
4) Go to DB and right Click and choose properties and press on Permissions and select the user and check all CheckBoxes under Grant Header like the figure bellow
5) Now you want to know the port number on which the SQL server listen to
by doing like the two figures bellow
from the previous figure you can get the port.
-------------------------------------------
Now Creating Servlet
1) Run Netbeans and press on "File" tab and press on "New Project"
2) Select Java Web from Categories and Web Application from Projects
3) Type your project name and directory and the libraries directory
4) Choose GlassFish and JEE
5) Do like the figure bellow (let all of them empty)
Click finish, now you have a web project
*** Creating Servlet
on project name right click and select new Servlet
Do like the bellow two figures
Press finish and edit the
Add the sql db driver into libraries
downloadable from this site http://www.mediafire.com/?3ycmaelhoot
Look at the example bellow
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.servlet.annotation.WebServlet;
/**
*
* @author Mohammad
*/
//this to call the servlet from an ip address
//http://ipaddress:port/defaultSite/ServletCallableName
@WebServlet(name = "ServletCallableName", urlPatterns = {"/ServletCallableName"})
public class NewServlet extends HttpServlet {
private Connection conn;
private PrintWriter printWriter;
private StringBuffer stringBuffer;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String ua = request.getHeader("User-Agent");
stringBuffer = new StringBuffer();
CallableStatement callableStatment = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;"//1433 sql port look at
//figures bellow
+ "databaseName=WhoWantsToBeMillionaireQs;user=mohammad;password=moh");
callableStatment = conn.prepareCall("{call get_rando_rows(?)}");//call get_rando_rows: procedure name created on Sql serve DB, takes one parameter, the procedure runs 3 select statments and returns 3 results as three tables
callableStatment.setString("Count", "5");//the procedure parameter
boolean isExecuted = callableStatment.execute();//to execute the query
int rsCount = 0;
try {
do {
if (isExecuted) {
ResultSet rs = callableStatment.getResultSet();
rsCount++;
ResultSetMetaData rsmd = rs.getMetaData();
int numOfColumns = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= numOfColumns; i++) {
stringBuffer.append(rs.getString(i));
stringBuffer.append("|#|");
}
}
rs.close();
System.out.println();
isExecuted = callableStatment.getMoreResults();
}
} while (isExecuted);
callableStatment.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (SQLException ex) {
stringBuffer.append(" SQL Exception. ");
stringBuffer.append(ex.getMessage());
} catch (ClassNotFoundException cex) {
stringBuffer.append(" Class Not Found Exception ");
stringBuffer.append(cex.getMessage());
} catch (Exception ex) {
stringBuffer.append("Unknown Exception ");
stringBuffer.append(ex.getMessage());
}
response.setContentType("text/plain");
printWriter = response.getWriter();
printWriter.print(stringBuffer.toString());
printWriter.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Mohammad Abu Hmead
Palestine
21.07.2011
01:43 AM