<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this chapter we will teach you how to access a database using Struts 2 in simple steps. Struts is a MVC framework and not a database framework but it provides excellent support for JPA/Hibernate integration. We shall look at the hibernate integration in a later chapter, but in this chapter we shall use plain old JDBC to access the database.</p>
<p>The first step in this chapter is to setup and prime our database. I am using MySQL as my database for this example. I have MySQL installed on my machine and I have created a new database called &#8220;<b>DOJDB</b>&#8220;. I have created a table called &#8220;<b>USER</b>&#8220;and populated it with some values. Below is the script I used to create and populate the table.</p>
</div>
<p> ;</p>
<pre class="highlight">CREATE TABLE users ( 
 User_ID VARCHAR(10) NOT NULL, 
 UserName VARCHAR(10) NOT NULL, 
 PASSWORD VARCHAR(10) NOT NULL, 
 PRIMARY KEY (User_ID) 
 ) 
 
 
INSERT INTO `DOJDB`.`users` 
 (`User_ID`, 
 `UserName`, 
 `PASSWORD` 
 ) 
 VALUES 
 ('DOJ01', 
 'Dinesh', 
 'Sweety' 
 ); 
</pre>
<h2><b>Create Action:</b></h2>
<p>The action class has the properties corresponding to the columns in the database table. We have user, password and name as String attributes. In the action method, we use the user and password parameters to check if the user exists, if so , we display the user name in the next screen. If the user has entered wrong information, we send them to the login screen again.<br />
Following is the content of<i><b> LoginAction.java</b></i> file:</p>
</div>
<div id="ads-id" align="center"></div>
<pre class="highlight">package com.dineshonjava.struts2.login; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@SuppressWarnings("serial") 
public class LoginAction extends ActionSupport{ 
 private String username; 
 private String password; 
 private String userid; 
 
 public String execute() { 
 String ret = ERROR; 
 Connection conn = null; 
 
 try { 
 String URL = "jdbc:mysql://localhost:3306/DOJDB"; 
 Class.forName("com.mysql.jdbc.Driver"); 
 conn = DriverManager.getConnection(URL, "root", "root"); 
 String sql = "SELECT name FROM user WHERE"; 
 sql+=" userName = ? AND password = ?"; 
 PreparedStatement ps = conn.prepareStatement(sql); 
 ps.setString(1, username); 
 ps.setString(2, password); 
 ResultSet rs = ps.executeQuery(); 
 
 while (rs.next()) { 
 username = rs.getString(1); 
 ret = SUCCESS; 
 } 
 } catch (Exception e) { 
 addActionError(getText("error.login")); 
 ret = ERROR; 
 } finally { 
 if (conn != null) { 
 try { 
 conn.close(); 
 } catch (Exception e) { 
 } 
 } 
 } 
 return ret; 
 } 
 
 public String getUsername() { 
 return username; 
 } 
 
 public void setUsername(String username) { 
 this.username = username; 
 } 
 
 public String getPassword() { 
 return password; 
 } 
 
 public void setPassword(String password) { 
 this.password = password; 
 } 
 
 public String getUserid() { 
 return userid; 
 } 
 
 public void setUserid(String userid) { 
 this.userid = userid; 
 } 
 
} 
 
</pre>
</div>
<h2><b>Create main page:</b></h2>
<p>Now, let us create a JSP file<i><b> Login.jsp</b></i> to collect the <b>username </b>and <b>password</b>. This username and password will be checked against the database.</p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
<;head>; 
<;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">; 
<;title>;Struts 2 - Login Application | dineshonjava.com<;/title>; 
<;/head>; 
<;body>; 
<;h2>;Struts 2 - Login Application<;/h2>; 
<;font color="red">;<;s:actionerror />;<;/font>; 
<;s:form action="login/welcome.action" method="post">; 
 <;s:textfield name="username" key="label.username" size="20" />; 
 <;s:password name="password" key="label.password" size="20" />; 
 <;s:submit method="execute" key="label.login" align="center" />; 
<;/s:form>; 
<;/body>; 
<;/html>; 
</pre>
</div>
<h2><b>Create Views:</b></h2>
<p>Now let us create <i><b>success.jsp</b></i> file which will be invoked in case action returns <b>SUCCESS</b>, but we will have another view file in case of an <b>ERROR </b>is returned from the action.</p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
<;head>; 
<;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">; 
<;title>;Welcome<;/title>; 
<;/head>; 
 
<;body>; 
 <;h2>;Hello Welcome , <;s:property value="username" />;...! Dineshonjava.com<;/h2>; 
 
<;/body>; 
<;/html>; 
</pre>
</div>
<h2><b>Configuration Files:</b></h2>
<p>Finally, let us put everything together using the <b>struts.xml</b> configuration file as follows:</p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8" ?>; 
<;!DOCTYPE struts PUBLIC 
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
 "http://struts.apache.org/dtds/struts-2.0.dtd">; 
 
<;struts>; 
 <;constant name="struts.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="login" extends="struts-default" namespace="/login">; 
 <;action name="welcome" class="com.dineshonjava.struts2.login.LoginAction">; 
 <;result name="success">;/Welcome.jsp<;/result>; 
 <;result name="error">;/Login.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
 
 <;/struts>; 
</pre>
</div>
<p><b>myapp.properties</b></p>
<pre class="highlight">label.username= Username 
label.password= Password 
label.login= Login 
error.login= Invalid Username/Password. Please try again. 
</pre>
<p> ;</p>
</div>
<p>Following is the content of <i><b>web.xml</b></i> file:</p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Struts2MyFirstApp<;/display-name>; 
 <;filter>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;filter-class>; 
 org.apache.struts2.dispatcher.FilterDispatcher 
 <;/filter-class>; 
 <;/filter>; 
 <;filter-mapping>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 <;welcome-file-list>; 
 <;welcome-file>;Login.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
</pre>
<p>Now, right click on the project name and click <b>Export >; WAR</b> File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access</p>
<p>URL <i><b>http://localhost:8080/doj/Login.jsp.</b></i></p>
<p>This will give you following screen:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/6666.png" border="0" /></div>
</div>
<p><b>Enter a wrong user name and password. You should see the next page-</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/77777.png" border="0" /></div>
<p> ;</p>
</div>
<p><b>Now enter &#8220;<i>Dinesh</i>&#8221; as user name and &#8220;<i>Sweety</i>&#8221; as password. You should see the next page-</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/8888-1.png" border="0" /></div>
<p> ;</p>
</div>
<h2><b>Download Source Code+Libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2Database.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Struts2Database.zip</a></b></h2>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/struts-2-file-upload-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/struts-2-sending-mails/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/struts-2-file-upload-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/struts-2-sending-mails/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '384'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…