MVC architecture example

MVC architecture example

View Answers

September 4, 2008 at 5:13 PM

HI,

Follow the procedure
1.Create a jsp page name like login.jsp(remember in login.jsp in action you will put the servlet name like action="login.java")
2.Then create a servlet page name like login.java
3.Then create another java class Login.java(It the same name of the sevlet)where get and set mathod will be used.

4.Then create another java class where you will put your database connectivity logic.


Go through the code and create the files USING netbeans ide or eclipse then
enjoy :)))))))))))

here is the code for jsp page like login.jsp(omit the css thingd just pick up the html code and make page by yourself)






<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="fr"><head><title>Secure Networks Technologies - Web Server</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<link rel="stylesheet" href="cryptonet_files/config.css" type="text/css">
<style type="text/css">
.style1 {
color: red;
}
</style><script charset="utf-8" id="injection_graph_func" src="cryptonet_files/injection_graph_func.js"></script></head><body>

<div id="container">

<div id="banner"> <img src="cryptonet_files/bluetooth.jpg" alt="" border="1" height="100" width="798">
<img src="cryptonet_files/Safe%2520Server.jpg" alt="" border="1" height="100" width="798">
</div>

<div id="outer">
<div id="inner">
<div id="left" style="">

<br>
<strong>
</strong><pre><a href="index.jsp"><img src="images/home.png" border="0" width="32" height="32" > &nbsp;&nbsp;</a><span class="style1"><a href="index.jsp"> <font color = red> Home </font> </a></span>
</pre>

<p>
<br>
<br>

</p>
</div>
<div id="content" style="">

<h1> Login</h1>

<p class="MsoNormal">
<span lang="EN-US">
<br>
</span>
</p><p><span lang="EN-US"></span></p>
<span lang="EN-US"><table border="0" cellpadding="0" cellspacing="0">

<!-- CryptoNet Section ----------------------------------------------------- -->
<tbody><tr> <td colspan="2"> <table width="476" border="0">
<tr>
<td width="186">User Name </td>
<td width="274">
<form name="form2" id="form2" method="post" action="login.java">
<input type="text" name="textfield" />
</form></td>
</tr>

<tr>
<td>Password</td>
<td><input type="text" name="textfield2" /></td>
</tr>



<tr>
<td>&nbsp;</td>
<td>&nbsp;<span lang="EN-US"><span lang="EN-US"><span lang="EN-US"><span lang="EN-US">
<input type="submit" name="Submit" value="Login" />
</span></span></span></span></td>
</tr>
</table> <strong> </strong></td></tr>
<!-- blank line -->
<!-- blank line -->

<!-- BotNet Section ----------------------------------------------------- -->
<!-- blank line -->
<!-- blank line -->

<!-- WirelessNet Section ----------------------------------------------------- -->
<!-- blank line -->

<tr> <td width="60"> </td>
<td align="west" valign="top" width="500">
<form name="form1" id="form1" method="post" action="">
<span lang="EN-US"><span lang="EN-US"><span lang="EN-US"><span lang="EN-US">
</span></span></span></span> <span lang="EN-US"><span lang="EN-US">
</span></span>
</form></td>
</tr>
</tbody></table>
<span lang="EN-US"></span><br>
<br>
</span>
<p>&nbsp; </p>

</div>
<!-- end content -->

</div>
<!-- end inner -->
</div><!-- end outer -->

<div id="footer"> Last updated on May 02, 2008</div>

</div><!-- end container -->

</body>
</html>



login.java servlet code is given below:

package com.servlet;

import java.io.IOException;
import com.model.idms.Login;
import com.model.idms.actions.IDMSDelegate;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author hasan
*/
public class login extends HttpServlet {

public login(){
super();
}
/**
* 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 {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

String username=request.getParameter("username");
String password=request.getParameter("password");

//Create the instance of the Login class
Login login=new Login();
login.setusername(username);
login.setpassword(password);

//create the instance of the IDMS Delegate

IDMSDelegate idmsdelegate=new IDMSDelegate();
idmsdelegate.addlogin(login);





HttpSession session=request.getSession(true);
session.setAttribute("loginobject",login);


getServletConfig().getServletContext().getRequestDispatcher("/submit_app.jsp").forward(request,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.
*/


}



This is the code of Login.java where the set and get method is given below:


package com.model.idms;

/**
*
* @author hasan
*/
public class Login {

private String firstname;
private String lastname;
private String email;
private String username;
private String password;
private String contract_type;
//private String identification_type;
private String creditcard;
private String co;
private String zipcode;
private String identification;
private String number;
private String contract;
private String street;
private String country;
private String language;
private String amount;
private String interval;
private String address;
private String city;
private String cellphone;
private String homephone;
private String currency;
private String payment;
private String frequency;

public void sethomephone(String homephone){

this.homephone=homephone;
}
public String gethomephone(){

return homephone;
}

public void setfrequency(String frequency){

this.frequency=frequency;

}

public String getfrequency(){

return frequency;
}

public void setcountry(String country){

this.country=country;

}

public String getcountry(){

return country;
}

public void setlanguage(String language){

this.language=language;
}

public String getlanguage(){

return language;
}

public void setinterval(String interval){

this.interval=interval;
}
public String getinterval(){

return interval;

}

public void setcity(String city){

this.city=city;
}

public String getcity(){

return city;

}

public void setamount(String amount){

this.amount=amount;
}
public String getamount(){

return amount;
}
public void setpayment(String payment){

this.payment=payment;
}
public String getpayment(){

return payment;
}
public void setaddress(String address){

this.address=address;
}
public String getaddress(){

return address;
}
public void setcurrency(String currency){

this.currency=currency;
}
public String getcurrency(){

return currency;
}
public void setcellphone(String cellphone){

this.cellphone=cellphone;

}
public String getcellphone(){

return cellphone;
}







public void setstreet(String street){

this.street=street;
}
public String getstreet(){

return street;
}


public void setco(String co){

this.co=co;
}

public String getco(){

return co;

}

public void setzipcode(String zipcode){

this.zipcode=zipcode;
}

public String getzipcode(){

return zipcode;
}

public void setidentification(String identification){

this.identification=identification;
}
public String getidentification(){

return identification;
}

public void setnumber(String number){

this.number=number;
}
public String getnumber(){

return number;
}

public void setcontract(String contract){

this.contract=contract;
}
public String getcontract(){

return contract;
}
public void setcreditcard(String creditcard){

this.creditcard=creditcard;

}
public String getcreditcard(){

return creditcard;
}
public void setcontract_type(String contract_type){
this.contract_type=contract_type;
}
/*public void setidentification_type(String identification_type){

this.getidentification_type=identification_type;
}
*/

public void setfirstname(String firstname){
this.firstname=firstname;

}

public String getfirstname(){
return firstname;
}

public void setlastname(String lastname){
this.lastname=lastname;
}

public String getlastname(){
return lastname;

}

public void setemail(String email){
this.email=email;
}
public String getemail(){
return email;
}


public void setusername(String username) {
this.username=username;
}


public String getusername(){

return username;
}

public void setpassword(String password){
this.password=password;
}

public String getpassword(){

return password;
}

}



here is the IDMSDelegate.java





package com.model.idms.actions;

import com.model.idms.Login;
import com.utility.DBHandlerIdms;
import java.sql.ResultSet;


/**
*
* @author hasan
*/
public class IDMSDelegate {

private DBHandlerIdms db = new DBHandlerIdms();





private ResultSet rs = null;

public boolean addlogin(Login login){
try{

db.makeconnection();
//String query= "insert into login(username,password) values('"+Login.getusername()+"','"+Login.getpassword()+"')";





String query = "insert into login (username,password) values ('"+login.getusername()+"','"+login.getpassword()+"' )";
int a=db.executeUpdate(query);

if(a>0){
db.releaseConnection();

return true;

}
else{
db.releaseConnection();
return false;
}


}catch(Exception ex){
ex.printStackTrace();
return false;
}



}

}




And here is the IDMDHandler.java



package com.utility;
import java.sql.*;


/**
*
* @author hasan
*/
public class DBHandlerIdms {

public Connection connection;
public Statement stmt;
public ResultSet rs;

public String user="root";
public String pass="admin";

public String url="jdbc:mysql://localhost:3306/idms";;
public String driver="com.mysql.jdbc.Driver";



//public String url="jdbc:mysql://localhost:3306/idms";;
//public String driver="com.mysql.jdbc.Driver";

public DBHandlerIdms(){
}

public Connection makeconnection()throws Exception{
Class.forName(driver);
connection=DriverManager.getConnection(url,user,pass);
return connection;
}



public void releaseConnection()throws Exception{
connection.close();

}
public ResultSet executeQuery(String query)throws Exception{

rs=null;
stmt=connection.createStatement();
rs=stmt.executeQuery(query);
return rs;
}
public int executeUpdate(String query)throws Exception{
int rowaffected=0;
stmt=connection.createStatement();
rowaffected=stmt.executeUpdate(query);
return rowaffected;
}

}















September 4, 2008 at 9:52 PM

Hi friend,



Read for more information.

http://www.roseindia.net/jsp/loginbean.shtml


Thanks.









Related Tutorials/Questions & Answers:
MVC architecture example - Java Beginners
MVC architecture example  hi.. I would like to ask the example on Login authentication using MVC model. I really don't understand how it work. Can u give me some example of login authentication using JSP+Servlet+Javabeans
mvc architecture - Java Beginners
mvc architecture  i am asking you give me an example for the object oriented programming using MVC architecture then my problem is railway tickect... to that arraylist. for this one i have to do code in MVC architecture
Advertisements
mvc architecture - Java Beginners
mvc architecture  can u please provide a example program with code using mvc architecture mine problem is adding routes for the railwaystation... on the MVC architecture please send me the link for knowing the implementation code
MVC Example
MVC Example  I WANT MVC EXAMPLE PROGRAM using Jsp Servlets and Jdbc with mysql of Insert,update,delete,search. please give the answer in MVC rule
MVC Architecture In Java
MVC Architecture In Java In this section we will discuss about the MVC architecture. This section will describe you all the aspects of MVC architecture..., what is MVC architecture, what is model, what is view, what is controller
MVC Architecture
MVC Architecture       The main aim of the MVC architecture  is to separate the business... are the reasons why we should use the MVC design pattern.ADS_TO_REPLACE_1
About MVC 2 architecture and jsp - JSP-Servlet
About MVC 2 architecture and jsp  Sir, I want to know about MVC 2 architecture and can we bring all functionalities of servlets in jsp.What is the need of servlets when jsp can perform all functionalities .  Hi
online book store by using MVC architecture
online book store by using MVC architecture  Tarzan book store... and servlets The application needs to be built in J2EE and complaint with MVC architecture. (adsbygoogle = window.adsbygoogle || []).push
using jsp and servlets andin Mvc architecture
using jsp and servlets andin Mvc architecture  how to insert a form data in different tables, that means education details in education table, address details in address table,like that and finally if we fetch all must come
Spring MVC User Registration example
Spring MVC User Registration example  hi, I am unable to find springMVCUserRegistration example full code can u send me full code of this example   Please visit the following link: Spring MVC USer Registration
JSF Architecture
also uses the MVC pattern. ?JSF Architecture? article in roseindia.net explains the architecture of a JSF application. Read about the topic at JSF...MVC is one of the most widely used design pattern for developing applications
Query for generating Stock Report Using MVC architecture in j2ee
Query for generating Stock Report Using MVC architecture in j2ee  help Me for query , to generate the stock report of shopping project. Considering some data (current stock=(summation of)watch Quantity in WATCHMASTER
Problem in populating tableview with database data using javafx MVC architecture.
Problem in populating tableview with database data using javafx MVC architecture.  This code is working in simple javafx appliction without MVC architecture. Here i want to populate tableview as it is with database table data
Problem in populating tableview with database data using javafx MVC architecture.
Problem in populating tableview with database data using javafx MVC architecture.  This code is working in simple javafx appliction without MVC architecture. Here i want to populate tableview as it is with database table data
Hibernate Architecture
In this tutorial we are going to describe hibernate architecture with example
Spring MVC Hello World Example
Spring MVC Hello World Example       Spring MVC Hello World Example in Spring 2.5 In this tutorial we will develop the Spring MVC Example and then run on the Tomcat server. We
Spring 2.5 MVC User Registration Example
Spring MVC User Registration example       Spring 2.5 MVC User Registration Example This tutorial shows you how to create user registration application in Spring MVC. This application
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example  Spring 3 MVC Login Form Example index.jsp file have code.... Simple Form request will be handle by SimpleFormController @Controller public class @Controller public class
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example  Spring 3 MVC Login Form Example index.jsp file have code.... Simple Form request will be handle by SimpleFormController @Controller public class @Controller public class
Request for complete code of the Spring 2.5 MVC User Registration Example
Request for complete code of the Spring 2.5 MVC User Registration Example  Hi, The Spring 2.5 MVC User Registration Example is very helpfull... the complete code of this example or rest of the part. Best Regards, Uttam kumar
Spring 3 MVC Login Form Example with Database MySql
Spring 3 MVC Login Form Example with Database MySql  Sir i have checked your project of Spring 3 MVC Login Form Example But Sir Not able to do It with database Mysql. Can you Provide code for login with database. Thanks
Spring 3 MVC hello world, Spring 3.0 Web MVC Example
Spring 3 MVC Hello World Example Getting stated with the Spring 3 MVC by developing first Hello World MVC example. This is the first example of writing first example in Spring MVC. After completing the tutorial you will be able
MVC - Struts
MVC  CAN ANYONE GIVE ME A REAL TIME IMPLEMENTATION OF M-V-C ARCHITECTURE WITH A SMALL EXAMPLE......  Hi friend, Read for more information. http://www.roseindia.net/struts/mvc-architecture.shtml Thanks
BeanNameUrlHandlerMapping example in Spring 2.5 Web MVC framework
BeanNameUrlHandlerMapping Example       BeanNameUrlHandlerMapping example in Spring 2.5 MVC framework.... In this example we will discuss about BeanNameUrlHandlerMapping that provide
Spring 3.0 MVC Form, Spring 3.0 MVC Form Example
Spring 3 MVC Form Example In this tutorial we are going to develop Spring 3 MVC form example. This  application will help you learn the concept of Spring 3 MVC forms. After completing this tutorial you will be able to understand
Spring Architecture
Spring Architecture Spring is a light-weight framework that is used for the development of Enterprise-ready applications. The architecture of Spring forms a layered architecture having seven modules. All modules are built over core
Problem in Spring 3 MVC and Hibernate 3 Example tutorial source codes
Problem in Spring 3 MVC and Hibernate 3 Example tutorial source codes  I referred your tutorial "Spring 3 MVC and Hibernate 3 Example" and downloaded your source codes. I created a build.xml and built it using ant. And copied
Spring MVC Say Hello Example
Say Hello application in Spring 2.5 MVC   ... with the development of form based application in Spring MVC. You will learn how.... Then application displays name with "Hello" message. For example we will enter
mvc
mvc  I want MVC example using jsp,servlets,pojoclass,jdbc(with mysql)..operation of insert,search,delete,update. I want above web application only in "Model view controller" format. last time i asked the same question, but i
Understanding MVC design pattern
for specific project. The MVC architecture contains the three parts they are Model...Understanding MVC Design Pattern The MVC (Model View Controller) design pattern is used by the struts for building an applications. It makes the application
Spring 3.2 MVC Form Handling
In this example, you will learn about handling forms in Spring 3.2 MVC
Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code
Video Tutorial of Spring 4 MVC: This video tutorial teaches you how to write Spring 4 MVC Hello World example and run on Tomcat Server In this video... information will help you master the example given here easily. Spring 4 MVC
mvc
mvc   can any one tell me how to save the data in the data base sql and after saving the data how to retrieve the data according to some condition and show it in another form... iam using eclipse hibernate and java ... iam using
Spring MVC JDBC Example
Spring MVC JDBC Example To connect your Spring web application to the database you will have to create a DataSource and JdbcTemplate bean in your dispatcher servlet. The following is the mapping for your data source in a dispatcher
Spring MVC XmlViewResolver Example
-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base
Architecture of Struts 2 Framework
Architecture of Struts 2 Framework In this chapter we will discuss about the Architecture and Lifecycle of Struts 2. This Struts 2 Architecture is based on WebWork and XWork framework. Struts 2 promotes the MVC framework
Spring MVC Login Example
Spring MVC Login example       Spring 2.5 MVC User Login Example This section explains you how you can make Login example using Spring MVC module. In this login example we are not connecting to any
Spring 3.2 MVC Hibernate Example
In this section, you will learn to integrate Spring 3.2 MVC with Hibernate
JSF architecture
JSF architecture  What is JSF architecture
Java Architecture
Java Architecture  What is architecture of java
Spring MVC
MVC Login Form Example Spring 3 MVC Validation Example Spring 3 MVC Registration Form Example Spring MVC JDBC Example Thanks...Spring MVC  Hi, What do you understand by Spring MVC? Thanks  
Spring MVC
MVC Login Form Example Spring 3 MVC Validation Example Spring 3 MVC Registration Form Example Spring MVC JDBC Example Thanks...Spring MVC  Hi, What do you understand by Spring MVC? Thanks  
Spring 3 MVC Validation Example
Spring 3 MVC Validation Example This tutorial shows you how to validate Spring 3 MVC based applications. In Spring 3 MVC annotation based controller has been added and other type of controllers are not deprecated. In this example
AbstractController example
AbstractController example in Spring web MVC framework       AbstractController example in Spring web MVC framework In this tutorial we are discussing AbstractContoller
MVC in Java
_TO_REPLACE_1 Moreover, we can also say that MVC is a software architecture...MVC in Java Before understanding the use of MVC in Java, let's understand what is MVC? Abbreviated as MVC, Model-View-Controller is a term used
Java Architecture.
Java Architecture.  Explain Java Architecture.   Please visit the following links: http://www.roseindia.net/java/quick-java.shtml http://www.roseindia.net/java/java-virtual-machine.shtml
spring MVC frame work - Spring
spring MVC frame work  Example of spring MVC frame work
Java Architecture
Java Architecture  What is architecture of java   Architecture of Java program Here is an architecture of a java program where documentation part or writing comments into program is completely optional.Declaration
Struts Architecture - Struts
(MVC) design pattern. It uses and extends the Java Servlet API to encourage developers to adopt an MVC architecture. Struts framework provides three key... struts architecture with flow.   Hi friend, Struts is an open source
MVC in flex
MVC in flex  Hi..... Please tell me What is MVC and how do you... application according to MVC. Thanks  Ans: Model?View?Controller (MVC) is a software architecture, currently considered an architectural pattern used

Ads