Register Login
Internet / AI Technology University (ITU/AITU)





|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 4. Web Apps Frameworks >> 4.4. Spring with Apache Maven and Data Service Frameworks >> 4.4.4.Data Service Framework and Troubleshooting
Current Topic: 4.4.4.3.A sample of DataService Action
Sub-Topics: 4.4.4.3.1.Artificial Intelligence Applications | 4.4.4.3.2.Climate Change Impacts | 4.4.4.3.3.Mental Health Awareness
-- Scroll to check for more content below...
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
4.4.4.3.The action sample with DataService framework
This is a sample of a typical action that works with the DataService framework.

Do we need to specify the name of the action in the web.xml file?

No!
There is a single Servlet Dispatcher class, ItsBaseServlet, which passes control to a proper action as we described in the previous lesson.
In the DataService framework we use web.xml to define some parameters for the application.

Here is the web.xml file:




4.4.4.3.DataServiceWeb


pathToDeploy
c:/tomcat7/webapps/



appName
4.4.4.3.DataServiceWeb



com.its.util.ItsBaseServlet



com.its.util.ItsBaseServlet
com.its.util.ItsBaseServlet


com.its.util.ItsBaseServlet
/Lookup



login.jsp



404
/jsp/notFound.jsp




The welcome-file-list section defines the page that will be displayed by default.
We choose the login.jsp page, but we will add the login page as well as the welcome page in the next lesson.
Here we only provide the error page, which must be displayed on the wrong request, which does not point to any existing action or pages.
The error page, which we call notFound.jsp can look this way:




No Records Were Found.
Please Clarify Your Request

Welcome back to known places




Was it clear so far? Highlight the text in question Or


Every action must implement com.its.util.BaseAction interface,which has a single method:
public String[] run(HttpServlerRequest request, HttpServletResponse response);

Naturally, every action starts similar to the sample below:

/**
* The LoginAction handles input from login.jsp, where a user can enter a name and a login.
* If name and login are found in the database, the action passes control to the welcome.jsp.
* In the case of no match, the action passes control back to login.jsp with the error message
*/
public class LoginAction implements BaseAction {
public String[] run(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String login = request.getParameter("login");
String password = request.getParameter("password");
String name = retrieveNameFromDB(login, password);
if(name == null) {
request.setAttribute("html",
"No match for login and password found. Try again...");
return new String[] {"login.jsp"};
} else {
request.setAttribute("html", "Welcome "+name+"!");
return new String[] {"welcome.jsp"};
}
} // end of method


Of course, you noticed the method with a promising name retrieveNameFromDB.
In the next lesson we will work with a database in DataService framework
So far we will provide a temporary stub, the method, which always returns null, making upset a user, which login and password do not match.


/**
* This method is a temporary stub, till we can work with the database
* @param login
* @param password
* @return name
*/
public String retrieveNameFromDB(String login, String password) {
// currently always returns null
return null;
} // end of method
} // end of class



This is almost complete application that even can work. Although its work will be limited by going to the same error page again and again, just because other pages are not in place yet.

With this prediction let us follow the assignments and build the application.

Assignments:
1. Open Eclipse and create a new Dynamic Web Project with the name 4.4.4.3.DataServiceWeb.
2. Find the file web.xml in the WebContent/WEB-INFO and provide the content of the file displayed above.
3. In the WebContent create NEW – Folder with the name jsp.
4. In this folder create NEW – JSP page with the name notFound.jsp and provide there the content that is displayed above.
5. In the Java Resources under the src create a NEW – Package with the name com.its.util.
6. From the previous project 4.4.4.2.ItsBaseServlet from the same package copy both files BaseAction and ItsBaseServlet to this package in the new project.
7. Switch the Console window to the Server tab, right-mouse click on the Tomcat Server link and select the Add and Remove option to add to the right side the 4.4.4.3.DataServiceWeb project. Make sure it is the only project on the right side.
8. Then, again right-mouse click on the Tomcat Server link and select the Publish option. This should publish the project to the c:/tomcat7/webapps/ directory according to the setting in the Tomcat Server configuration, which is visible on double-click on the Tomcat Server link.
9. Check with Windows Explorer that the project is actually landed at c:/tomcat7/webapps. If this not happened, come back to the Troubleshooting lesson. Otherwise Run the project by right-mouse click – Run.
10. Open a browser and type the URL: http://localhost/4.4.4.3.DataServiceWeb/Lookup
11. Create 2 QnA related to the lesson and email to dean@ituniversity.us
12. Describe your experience (what was easy and difficult) of creating the project and playing with the browser in a brief essay and email it as MS Word to dean@ituniversity.us

We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!

Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/><?xml version="1.0" encoding="UTF-8"?>
<br/><web-app>
<br/>
<br/>  <display-name>4.4.4.3.DataServiceWeb</display-name>
<br/><!-- Define the path where the application is deployed -->  
<br/>   <context-param>
<br/>    <param-name>pathToDeploy</param-name>
<br/>    <param-value>c:/tomcat7/webapps/</param-value>
<br/>   </context-param>
<br/>   <!-- Project Name -->
<br/>   <context-param>
<br/>    <param-name>appName</param-name>
<br/>    <param-value>4.4.4.3.DataServiceWeb</param-value>
<br/>   </context-param>
<br/><!-- The listener class responsible for application and session init and destroy  -->
<br/>  <listener>
<br/>		<listener-class>com.its.util.ItsBaseServlet</listener-class>
<br/>  </listener>
<br/><!-- The single servlet class that plays the role of a dispatcher -->  
<br/>  <servlet>
<br/>    <servlet-name>com.its.util.ItsBaseServlet</servlet-name>
<br/>    <servlet-class>com.its.util.ItsBaseServlet</servlet-class>
<br/>  </servlet>
<br/>  <servlet-mapping>
<br/>    <servlet-name>com.its.util.ItsBaseServlet</servlet-name>
<br/>    <url-pattern>/Lookup</url-pattern>
<br/>  </servlet-mapping>
<br/><!-- The list of files to open by default -->  
<br/>  <welcome-file-list>
<br/>    <welcome-file>login.jsp</welcome-file>
<br/>  </welcome-file-list>
<br/><!-- Define the ERROR page to display on a wrong request -->  
<br/>  <error-page>
<br/>    <error-code>404</error-code>
<br/>    <location>/jsp/notFound.jsp</location>
<br/>  </error-page>
<br/></web-app>
<br/>


The welcome-file-list section defines the page that will be displayed by default.
We choose the login.jsp page, but we will add the login page as well as the welcome page in the next lesson.
Here we only provide the error page, which must be displayed on the wrong request, which does not point to any existing action or pages.
The error page, which we call notFound.jsp can look this way:
<br/>
<br/>
<br/><h2 style="text-align:center">
<br/>	No Records Were Found.
<br/>	Please Clarify Your Request
<br/>
<br/><a href="javascript:history.back()">Welcome back to known places</a>	
<br/></h2>
<br/>
<br/>
<br/>






Was it clear so far? Highlight the text in question

Or



Every action must implement com.its.util.BaseAction interface,which has a single method:
public String[] run(HttpServlerRequest request, HttpServletResponse response);

Naturally, every action starts similar to the sample below:
<br/>/**
<br/> * The LoginAction handles input from login.jsp, where a user can enter a name and a login.
<br/> * If name and login are found in the database, the action passes control to the welcome.jsp. 
<br/> * In the case of no match, the action passes control back to login.jsp with the error message 
<br/> */
<br/>public class LoginAction implements BaseAction {
<br/>	public String[] run(HttpServletRequest request, HttpServletResponse response)
<br/>			throws Exception {
<br/>		String login = request.getParameter("login");
<br/>		String password = request.getParameter("password");
<br/>		String name = retrieveNameFromDB(login, password);
<br/>		if(name == null) {
<br/>			request.setAttribute("html", 
<br/>"No match for login and password found. Try again...");
<br/>			return new String[] {"login.jsp"};
<br/>		} else {
<br/>			request.setAttribute("html", "Welcome "+name+"!");
<br/>			return new String[] {"welcome.jsp"};
<br/>		}
<br/>	} // end of method
<br/>


Of course, you noticed the method with a promising name retrieveNameFromDB.
In the next lesson we will work with a database in DataService framework
So far we will provide a temporary stub, the method, which always returns null, making upset a user, which login and password do not match.

<br/>	/**
<br/>	 * This method is a temporary stub, till we can work with the database
<br/>	 * @param login
<br/>	 * @param password
<br/>	 * @return name
<br/>	 */
<br/>	public String retrieveNameFromDB(String login, String password) {
<br/>		// currently always returns null
<br/>		return null;
<br/>	} // end of method
<br/>} // end of class
<br/>



This is almost complete application that even can work. Although its work will be limited by going to the same error page again and again, just because other pages are not in place yet.

With this prediction let us follow the assignments and build the application.

Assignments:
1. Open Eclipse and create a new Dynamic Web Project with the name 4.4.4.3.DataServiceWeb.
2. Find the file web.xml in the WebContent/WEB-INFO and provide the content of the file displayed above.
3. In the WebContent create NEW – Folder with the name jsp.
4. In this folder create NEW – JSP page with the name notFound.jsp and provide there the content that is displayed above.
5. In the Java Resources under the src create a NEW – Package with the name com.its.util.
6. From the previous project 4.4.4.2.ItsBaseServlet from the same package copy both files BaseAction and ItsBaseServlet to this package in the new project.
7. Switch the Console window to the Server tab, right-mouse click on the Tomcat Server link and select the Add and Remove option to add to the right side the 4.4.4.3.DataServiceWeb project. Make sure it is the only project on the right side.
8. Then, again right-mouse click on the Tomcat Server link and select the Publish option. This should publish the project to the c:/tomcat7/webapps/ directory according to the setting in the Tomcat Server configuration, which is visible on double-click on the Tomcat Server link.
9. Check with Windows Explorer that the project is actually landed at c:/tomcat7/webapps. If this not happened, come back to the Troubleshooting lesson. Otherwise Run the project by right-mouse click – Run.
10. Open a browser and type the URL: http://localhost/4.4.4.3.DataServiceWeb/Lookup
11. Create 2 QnA related to the lesson and email to dean@ituniversity.us
12. Describe your experience (what was easy and difficult) of creating the project and playing with the browser in a brief essay and email it as MS Word to dean@ituniversity.us


We invite you to create your own questions and answers (QnA) to increase your rank and win the Top Creativity Prize!


Topic Graph | Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to AI
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy