Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Web Application Developer’s Guide phần 5 doc
MIỄN PHÍ
Số trang
26
Kích thước
228.1 KB
Định dạng
PDF
Lượt xem
1664

Web Application Developer’s Guide phần 5 doc

Nội dung xem thử

Mô tả chi tiết

Tutorial: Creating a servlet that updates a guestbook 8-11

Step 6: Creating the data connection to the DBServlet

7 Click the Test Query button to test the query. If the query is successful,

the word Success displays to the right of the button. If the query cannot

be executed, an error message attempts to explain why the query failed.

8 Click OK to close the Query dialog box.

9 Click the Source tab to switch back to the editor.

Note You may see the following message displayed in the Designer tab of the

message pane:

Failed to create live instance for variable 'myDM' guestbookservlet.DataModule1

For now, you can ignore this message. You’ll fix this in a later step.

Right-click the Designer tab at the bottom of the AppBrowser and

choose Remove “Designer” Tab to remove the tab.

10 Click the Save All icon on the toolbar to save your work.

JBuilder adds the queryDataSet1.setQuery() method to the jbInit() method.

In the next step, you’ll set up the data connection to the DBServlet.

Step 6: Creating the data connection to the DBServlet

In this step, you’ll create a data connection to the DBServlet. The

connection allows the servlet to pass data to the data module.

1 Double-click DBServlet.java in the project pane to open it in the editor.

2 Find the line of code in the class definition that reads:

private static final String CONTENT_TYPE="text/html"

3 Position the cursor after this line of code and add the following line of

code:

DataModule1 dm = guestbookservlet.DataModule1.getDataModule();

4 Click the Save All icon on the toolbar to save your work.

Now that the servlet is connected to the data module, you need to make

both servlets and the data module do something. From this point on in our

tutorial, you’ll be entering code directly into the editor. The first step will

be to create an input form in FormServlet.

Step 7: Adding an input form to FormServlet

In this step, you’ll add code to the doGet() method of FormServlet. This

code creates a form using the HTML <form> tag. The form reads in two

values - UserName and UserComment - entered by the user. This data is posted

to DBServlet, the servlet that communicates with the data module.

8-12 Web Application Developer ’ s Guide

Step 8: Adding code to connect DBServlet to the data module

A <form> tag is a standard HTML tag that creates an input form to gather

data from and display information to a user. The tag contains action and

method attributes. These attributes tell the servlet what to do when the

form’s Submit button is pressed. In our tutorial, the action attribute calls

DBServlet. The method attribute posts the UserName and UserComment

parameters to DBServlet.

To add code to FormServlet,

1 Double-click FormServlet in the project pane to open it in the editor. (It

may already be open.)

2 Find the doGet method near the top of the file.

Tip You can search by positioning the cursor in the structure pane and

typing doGet.

3 Remove the following line of code from the doGet() method:

out.println("<p>The servlet has received a GET. This is the reply.</p>");

4 Add the following lines of code to the doGet() method, between the

open and close <body> tags:

out.println("<h1>Sign the guestbook</h1>");

out.println("<strong>Enter your name and comment in the input fields

below.</strong>");

out.println("<br><br>");

out.println("<form action=table method=POST>");

out.println("Name<br>");

out.println("<input type=text name=UserName value=\"\" size=20 maxlength=150>");

out.println("<br><br>");

out.println("Comment<br>");

out.print("<input type=text name=UserComment value=\"\" size=50 maxlength=150>");

out.println("<br><br><br><br>");

out.print("<input type=submit value=Submit>");

out.println("</form>");

Tip You can copy and paste this code directly in the editor, or copy it from

the sample in the samples/WebApps/GuestbookServlet folder of your

JBuilder installation.

5 Click the Save All icon on the toolbar to save your work.

In the next step, you’ll add code that connects DBServlet to the data

module.

Step 8: Adding code to connect DBServlet to the data module

In this step, you’ll add code to the DBServlet’s doPost() method that:

• Reads in the UserName and UserComment parameters from FormServlet.

• Calls the DataModule method that updates the Guestbook JDataStore,

passing UserName and UserComment parameter values.

• Calls the data module method that saves changes to the JDataStore.

Tải ngay đi em, còn do dự, trời tối mất!