Working with Database in Servlet Environments

chapter 12 page no s 338 347 n.w
1 / 18
Embed
Share

Learn how to utilize a database within servlet environments, including obtaining server references, object storage, and interaction with the database in mixed .NET/Java settings. Explore examples and best practices for seamless integration.

  • Servlet
  • Database
  • Java
  • .NET
  • Environment

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Chapter #12 Page No s:[338 347] Presentation by : Satya Naga Sai Bhargavi Kolli #28

  2. Overview JSP/ Servlet Example Using the Database in a Servlet Viewing Data Web Environments Working in a Mixed .NET/Java Environment Caveats Schema Evolution

  3. Using the Database in a Servlet A reference to the server can be obtained within any servlet from the servlet context using the key db4oserver. Listing 12-10 shows a servlet, StoreServlet, which gets the server reference and opens a client for that server. A Person object is constructed using values for name and age passed to the servlet in its HttpRequest, and the client stores it in the database. The values can be passed in the query string or from a form in a JSP. A suitable JSP would look exactly the same in the browser as the ASP.NET data entry form in Figure 12-3.

  4. Listing 12-10. The StoreServlet Class package com.db4o.dg2db4oweb; import com.db4o.*; // standard servlet imports omitted public class StoreServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String name = request.getParameter("name"); int age= Integer.parseInt(request.getParameter("age")); ServletContext context = getServletContext(); ObjectServer server=(ObjectServer)context.getAttribute("db4oServer"); ObjectContainer db = server.openClient();

  5. One question might occur to you at this point: what if you want to access the db4o server from a class that is not a servlet? There s no problem in JSPs (JSPs are compiled to servlets, and the servletContext is available to a JSP through the built-in application object). What about plain Java classes, though? It is quite common to use a framework where the persistence layer is contained in classes that are not servlets. In this case it should be possible for you to register and deregister the server object in an object of your own that is accessible from your Java code. This might, for example, be a singleton class that can hold the server object and provide it to clients when required and also to the application when it wants to close the server.

  6. Working in a Mixed .NET/Java Environment db4o stores the full class name with each object in the database.Class names are crucial for db4o.Unless it can find objects in the database that have exactly the same as the target object, it will not return any results from a query. The full name indicates the .NET namespace or Java package name. Therefore, you .NET and java applications must have matching namespace/package names. Even though there is one compilation, as .NET and java classes are named slightly differently.

  7. Java Example

  8. First part creates a new database in Java and adds two objects. No conversions are necessary because there are no objects in the database initially.

  9. Output of the test run Checking class names in Java Reading database in Java Adding new data in Java Enter the developer's name and age (e.g. 'Tom 44'): Tom Enter the developer's name and age (e.g. 'Tom 44'): Bill 32 Enter the developer's name and age (e.g. 'Tom 44'): stop Reading database in Java [Bill;32] [Tom;44]

  10. The test now opens the same database file in C#. Needs to convert the name of the Person class. Existing data can be read and two more objects are added.

  11. Checking class names in .NETRenaming com.db4o.dg2db4o.chapter12.to com.db4o.dg2db4o.chapter12.Person, dg2db4o Reading database in .NET [Bill;32] [Tom;44] Adding new data in .NET Please enter a name: Joe Please enter an age: 90 Please enter a name: Eve Please enter an age: 21 Please enter a name: stop Reading database in .NET [Joe;90] [Bill;32] [Tom;44] [Eve;21] -------------- Ending program

  12. The database file is opened again in Java. Conversions are needed. The Person class is converted and so are some internal db4o classes. Data added in Java and C# is read so that new data can be added.

  13. Checking class names in Java Renaming com.db4o.dg2db4o.chapter12.Person, dg2db4o to com.db4o.dg2db4o.chapter12.Person Renaming com.db4o.MetaField, db4o to com.db4o.MetaField Renaming com.db4o.MetaClass, db4o to com.db4o.MetaClass Renaming com.db4o.StaticField, db4o to com.db4o.StaticField Renaming com.db4o.StaticClass, db4o to com.db4o.StaticClass Reading database in Java [Joe;90] [Bill;32] [Tom;44] [Eve;21] Adding new data in Java Enter the developer's name and age (e.g. 'Tom 44'): Sue 29 Enter the developer's name and age (e.g. 'Tom 44'): Reading database in Java Stop Reading database in Java [Joe;90] [Bill;32] [Tom;44] [Eve;21] [Sue;29]

  14. Caveats Couple of issues when using db4o in mixed environment. The conversion process makes a significant change to the database schema so that one type of application can read the data. Concurrent access by .NET and Java clients is not likely to be reliable. converting class names is only helpful if the same namespaces and packages are available in .NET and Java. Case for namespace and also for db4o API classes.

  15. Schema Evolution It is also known as database refactoring In object- oriented systems which depend on relational database, schema changes can be problematic,because all the object/relational(O/R) mappings should be altered,which is difficult in such environment Schema changes differs in RDBMS and OODBMS But schema changes in RDBMS is easy when compared to the OODBMS

  16. Thank You

Related


More Related Content