Hello,
Iam working on website, technically this is my first j2ee website .. so iam trying my best here ..
I tried to make simple front-controller technique using servlets + JSP .. and this is wat i done :
index.java
Map event = new HashMap();
event.put("cats", new cats());
event.put("login", new login());
RequestDispatcher header = request.getRequestDispatcher("./header.jsp");
header.include(request, response);
try{
String action = request.getParameter("action");
event r =(event) event.get(action);
r.showPage(request, response);
}catch(Exception ex){
}
RequestDispatcher footer = request.getRequestDispatcher("./footer.jsp");
footer.include(request, response);
cats.java
public class cats extends HttpServlet implements event {
String redirectLink = "./?action=cats&do=manage";
List dos = new ArrayList();
database db = new database();
void cats(){
dos.add("add");
dos.add("manage");
dos.add("edit");
dos.add("del");
}
public void showPage (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.cats();
db._init();
if(request.getMethod().equalsIgnoreCase("get"))
{
doGet(request, response);
}
else
{
doPost(request, response);
}
}
private void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rr = request.getRequestDispatcher("./add_cat.jsp");
rr.include(request, response);
}
private void addProcess(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String catname = request.getParameter("catname");
db.statement.execute("insert into cats set catname = '" + catname + "'");
response.sendRedirect(redirectLink);
} catch (SQLException ex) {
out.println(ex.getMessage());
}
}
* implements event : event is interface contains some init
* database : class to handle database
well, first I would like to hear what u think about tehcnique or the structure of code .. is it ok ?
I dun like that switch part, but i guess i didnt wanna compilcate it anymore .. but i would be galde to hear more ideas ..
and the most important, now i need to pass variables from servlets to JSP ..
say i have got some data from database, already contained in variable " Name " .. how i pass it to file " show.jsp " ?
and wat if it's ResultSet ? I can pass the whole resultset and loop in the jsp ?
I already tried to use " usebean " and " set or get property " but juz .. blank page, then later i got the error " can't compile the JSP page .... etc "
no need to tell i tried many tutorials in google, juz got nothing ..
any ideas would be apreciated, thanks !