27 Mayıs 2007 Pazar

A simple Servlet Example

A simple java servlet which gets request from a web page as parameter, process the request, set response attributes and forwards to destination page
----------------------------------------------


/*
* LoginServlet.java
*
* Created on 23 Dec 2006 Saturday, 07:39
*/

package com.myairline.servlets;

import java.io.*;
import java.net.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.myairline.db.DatabaseConn;
import com.myairline.db.UserDB;
import com.myairline.type.User;
import com.myairline.common.MyConstants;

/**
*
* @author ozkansari
* @version
*/
public class LoginServlet extends HttpServlet {

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Page will be forwarded to :::
String destinationPage = "";

// Make database connection :::
DatabaseConn dbConn = new DatabaseConn();
Connection connection = dbConn.getConnection();

// get command parameter that will decide which operation is performed :::
String cmd = (String) request.getParameter("cmd");

// get session variable :::
HttpSession sessionVar = request.getSession(true);

// Base Path
String contextPath = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath+"/";

// Check command and perform the proper operation :::
if( cmd.equals("auth_user") )
{
// Define Variables :::
String userName, password;
UserDB userCheck = new UserDB();
User userCurrent = new User();
int authorizationResult ;

// Get form parameters :::
userCurrent.setUsername( (String) request.getParameter("username") );
userCurrent.setPassword( (String) request.getParameter("password") );
authorizationResult = userCheck.doAuthentication( userCurrent );

if( authorizationResult == MyConstants.LOGGED_IN ) {

sessionVar.setAttribute("userObject",userCurrent);

String rolename = userCurrent.getRolename();

if( rolename.compareToIgnoreCase("customer") == 0 ){
destinationPage = "/customer/customerHome.jsp";
} else if( rolename.compareToIgnoreCase("agency") == 0 ){
destinationPage = "/agency/agencyHome.jsp";
} else if( rolename.compareToIgnoreCase("admin") == 0 ){
destinationPage = "/admin/adminHome.jsp";
}

}
else {
// Set Attribute and destination Page :::
Integer reason = new Integer( authorizationResult );
request.setAttribute( "login", reason );
destinationPage = "/home.jsp";
}

}

// Clean your garbage :::
dbConn.closeConnection();

// Go to destination page :::
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destinationPage);
dispatcher.forward(request, response);

}

// <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
*/
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
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}


Database Connection Class

A class to make a mySQL database connection using java
----------------------------------------------


/*
* Created on 02 August 2005 Tuesday
*
*/


/**
* @author ozkan sari
*/

import java.sql.*;
import java.sql.DriverManager;


/**
*
*/
public class DatabaseConn {

private static Connection conn ;
private static final String DBconnName = "my_db/localhost";

public DatabaseConn()
{
String serverName = "localhost";
String dbName = "my_db";
String userName = "root";
String password = "my_pw";

String url = "jdbc:mysql://"+ serverName +"/"+ dbName ;

makeDbConnection( url, userName, password );
}

public Connection getConnection( )
{
return conn;
}


/* ***********************************************************************************************/
/* -- makeDbConnection() FUNCTION ---------------------------------------------------------------*/
/* ***********************************************************************************************/

public Connection makeDbConnection( String databaseURL, String user, String pw ) {

try {
// load database driver class
//Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Class.forName("com.mysql.jdbc.Driver").newInstance();

if( user == null || pw==null ) {
user = "root";
pw = "";
}

// connect to database
conn = DriverManager.getConnection(databaseURL , user, pw);

System.out.println("->Database connection established");

return conn;
}
// detect problems loading database driver
catch ( ClassNotFoundException classNotFound ) {
classNotFound.getMessage();
System.out.println("->DB PROBLEM: Cannot load DB driver");
}
// detect database access problems
catch ( SQLException sqlexception ) {
sqlexception.getMessage();
System.out.println("->DB PROBLEM: Cannot access DB");
}
// detect other problems
catch (Exception e) {
e.getMessage();
System.out.println("->EXCEPTION");
}

return null;

} // --end function makeDbConnection()-


/* ***********************************************************************************************/
/* -- closeConnection() FUNCTION ----------------------------------------------------------------*/
/* ***********************************************************************************************/

public void closeConnection()
{
try {
if( conn == null ) {
System.out.println( "->NO DB CONNECTION TO CLOSE" );
return;
}

conn.close();

if( conn.isClosed() == false ) {
System.out.println( "->DB COULD NOT BE CLOSED");
}
}
catch( SQLException e ) {
e.printStackTrace();
}

} // -end-method-closeConnection()

}


5 Mart 2007 Pazartesi

Mühendislikte Projenizi Pazarlayabilmek

You have all been engineers and you should have already learned how to solve problems. So, we will not teach you how to solve problems, but we will teach you how to sell your solutions. If you cannot sell your solution, there is no need to implement that solution.

Aynen aktarmak istedim. Resim İşleme (Image Processing) dersini aldığım hocam Cem Ünsalan bugün bir anısını anlattı. Yukarıda alıntısını yaptığım sözleri, yüksek lisansa başladıklarında ilk derste hocaları söylemiş.

Tercüme olarak, biz mühendisler problemlere bir şekilde çözüm üretmeyi biliyoruz ama o problemin çözümünü satamadıktan sonra o problemi çözmenin de bir anlamı yoktur. Bu yüzden bence biz mühendislerin sosyal yeteneklerini geliştirmeleri önemli bence. Bazıları biz pazarlamacı değiliz, çözümü satacak olan pazarlamacıdır diyebilir ama illa bu pazarlama işini dışarıya satış olarak algılamamalı, neticede bir çalışan olarak projenizi veya çözümünüzü patronunuza da satmanız gerekecek.

21 Ocak 2007 Pazar

Akademik Bilişim Günleri 2007

Dumlupınar Üniversitesi ev sahipliğinde Üniversitelerde bilgi teknolojileri konusunda ilgili grupları bir araya getirerek, bilgi teknolojileri altyapısı, kullanımı, eğitimi ve üretimini tüm boyutlarıyla tanıtmak, tartışmak, tecrübeleri paylaşmak ve ortak politika oluşturmak amacıyla ulusal boyutta 31 Ocak - 2 Şubat tarihleri arasında Akademik Bilişim 2007 Kongre düzenlenecektir.

Ayrıntılı bilgi için ab2007.dpu.edu.tr adresini ziyaret edebilirsiniz.

Bu konferansın benim memleketim Kütahya'da yapılacak olması ve üniversite semester tatiline gelmesi büyük şans. Geçen seneki konferansın konularını inceledim de gerçekten çok güzel bir etkinlik olacağa benziyor.

Microsoft Zirvesi 2007

2005'te düzenlenen 10.000 kişinin katıldığı Microsoft Zirvesi’nin 2. si 24 - 26 Ocak 2007 tarihleri arasında Lütfi Kırdar Kongre Merkezi’nde gerçekleştirilecek. Her biri kendi alanında uzman yerli ve yabancı konuşmacılar 9 farklı salonda 150'den fazla sunum yapacak. 2007 Office sistem, Windows Vista ve Dynamics AX lansmanlarıyla da zenginleşecek olan zirvede katılımcılar tüm Microsoft ürünlerini yakından görme fırsatını bulacakmış.

microsoftzirvesi2007.com adresinden ayrıntılı bilgi alabilirsiniz.