2013年9月20日星期五

JAVA novice , JSP pages how a show when it starts loading the background database data

Such background data , a total of three ,
id username password
1 small 123
2 small Xie 345
3 whining 348


When you open the JSP page is displayed when the need for order , as

Username: small
Password: 123
Username: Xie
Password: 345
Username: hum
Password: 348



------ Solution ------------------------------------ --------
beginner , then go directly to Google JDBC operations under it , quite simply , it is less than 10 lines of code to the end of the fighting.
------ For reference only -------------------------------------- -
I Baidu for a long time , really do not know how to show
------ For reference only ---------------------- -----------------
Useful struts do ? Words with struts directly cycle can be , if not the words with struts on the page to use for statement to loop output
------ For reference only --------------------------------------- < br>

no use struts, only use JSP + servlet

The following is the JSP do not know what went wrong
<%
List msgList = (List) request.getAttribute ("msgList");
if (msgList == null) msgList = Collections.EMPTY_LIST;
%>

<%
messageBoard msgBoard;
for (int i = 0; i msgBoard = (messageBoard) msgList.get (i);
%>






<%
}
%>


------ For reference only ---------------------------------- -----


it too . . . .

I do not know what database you are , here is an Oracle database


<%@ page language="java" import="java.io.*,java.sql.*,javax.sql.* ,javax.naming.*"%>
<%@ page contentType="text/html;charset=gb2312" %> 
<% 
String sDBDriver = "oracle.jdbc.driver.OracleDriver"; 
String sConnStr = "jdbc:oracle:thin:@192.168.0.58:1521:ORCL";

Connection conn = null; 
ResultSet rs = null; 
Statement stmt = null; 

Class.forName(sDBDriver); 
conn = DriverManager.getConnection(sConnStr,"veh_admin","oracle"); 
stmt = conn.createStatement(); 

rs = stmt.executeQuery("select * from 表名字"); 
while (rs.next()){ 
  out.write(" " + rs.getString(1) + " " +  + rs.getString(2)); 
  out.write("<br>"); 

rs.close();

------ For reference only ----------------------------------- ----
also get back into the time the page data through the servlet is to how to achieve , in the public void init () initialized here do ?
------ For reference only -------------------------------------- -


Thank you, I always thought it was to connect to the database where the servlet
------ For reference only --------------------- ------------------


from the MVC architecture , it is there in the Controller should operate the database and get the data model , JSP is responsible only for show .

But you are a beginner , then pass out first principles , and then seriously study MVC.
------ For reference only -------------------------------------- -
Bangding man upstairs , you should first figure out MVC three-tier architecture , this problem will be very clear
------ For reference only ---------- -----------------------------

What if the servlet to how to achieve it ?
------ For reference only -------------------------------------- -
background check , save the data to a scope, and forwarded to the jsp read data after
------ For reference only --------------- ------------------------


Servlet will ResultSet data , assembled by line VO value object , and then stored in a List object , and finally put the List object into request.setAttribute ()

JSP request.getAttribute removed from the List, and then circulating List and remove the VO value object , on-demand .
------ For reference only -------------------------------------- -


Thank you , understand the point that the code does not know how to achieve .
------ For reference only -------------------------------------- -
first operated directly in front of the JSP JDBC run through , and then step by step, then try the code directly in the JSP will be divided into two parts:
1, is responsible for the ResultSet data assembled by line VO value object , and then stored in a List object , such msgList;
2, cycle msgList and remove VO value object, out.write showing the attributes.

this is successful, then try to say my 12th floor .
------ For reference only -------------------------------------- -
press "ldh911" boss said .
plus a special deal with the database layer , the operations on the database is added to that which , with the servlet call those functions , then el expressions in the page output.
------ For reference only -------------------------------------- -


Thank you
------ For reference only -------------------------------- -------
connect to the database in a servlet to do query that returns a list, request.setAttribute ("mylist", list). return to the page , the page request.getAttribute ("mylist"), then traverse , showing the kind of layout according to your amount on the line
------ For reference only -------------------------- -------------


Thank
------ For reference only --------------------------------- ------







------ For reference only ---------------------------------- -----
you want to display data from a database , we must first establish a connection, connect to the database. As in the servlet or struts class , or jsp pages is a design issue . The most stupid way is embedded inside jsp java code , connect to the database , writing queries will return results stored in the container, such as vector, and then display .
------ For reference only -------------------------------------- -

this guy handsome
------ For reference only -------------------------- -------------
a start do not call JSP, enter the page does not directly call Action get away ...
------ For reference only --- ------------------------------------
when the page is loaded with ajax asynchronous access background
------ For reference only ------------------------------------- -


codes are not standardized, general jsp pages do not pretend to connect to the database operation , put the servlet layer , if not used struts, then use el expression is a better choice. loaded first introduced .

------ For reference only ---------------------------------- -----
coot are awkward:
related to the page : first jsp page (index.jsp)-servlet (myservlet) - second jsp page (show.jsp)

on the first page (index.jsp) to write a code ( of course, you can pass some parameters )

database connection on the servlet check out a String [i] [j] are passed to the second page.

In the second page , show.jsp circulation print. if (j == 0) output name , if (j == 1) output password
------ For reference only ------------------- --------------------
  This reply was moderator deleted at 2012-03-02 09:05:53

------ For reference only ---------------------------------- -----
beginner it is recommended to write a servlet demo easy entry
------ For reference only ------------------ ---------------------
Home jsp include an action, or js inside the page load method to write a call to action inside the method ! !
------ For reference only -------------------------------------- -
lz, since you are a beginner, from now on we should develop coding habits .
1, your code no notes and no sentence empty .
2, if you want to achieve direct access to jsp, then check out the reality from the db in the data, as long as you have the mvc concept on it.
But upstairs said, to write in jsp write this should dao db connection information , very bad. This is not mvc.
properly should be : jsp ----- "servlet ----" service ( or direct dao) ------ "jsp show

service you can omit this layer .


in jsp to write one sentence:
so jump directly to the servlet.

in a servlet , doPost or doGet method , the two-step process :
(1), connect db, get the result set list. That is your course information .
(2), using out.print (); other forms of export html elements, ( a ) in the list come into circulation .
so , servlet dealt with, it will be the information you need in jsp reality .

------ For reference only ---------------------------------- -----
12th floor, you can try to say, page labels it

first not even the database , write a general list
------ For reference only ------------------------- --------------
lead a core tag library , less than three lines of code with cycle , and soon !
------ For reference only -------------------------------------- -
Thank you upstairs a few , I will come on hard
------ For reference only ---------------------- -----------------
send a code to give you an example about , is taken out of the database corresponding content :

JAVA类:


public class Lista extends ArrayList<Look> 
{
 ArrayList<Look> list=new ArrayList<Look>();
public ArrayList<Look> list() throws SQLException 
{
          
           
           ResultSet rs=null; 
           DB sqle=new DB();//专门的数据库类,数据库的一些操作都在这里面!
           String  sql="select * from message where id='"+(String)ActionContext.getContext().getSession().get("id")+"'";
           rs= sqle.db(sql);
           while(rs.next())
           {
            Look k=new Look();
            k.setZhuti(rs.getString(2));
            k.setLid(rs.getString(4));
            k.setTime(rs.getDate(5));
            list.add(k);
           }
           System.out.println("链表长度:"+list.size());
           return list;
}

DB数据库类:
public ResultSet db(String sqle)
    {
  
      try{ 
    Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
        con=DriverManager.getConnection("jdbc:odbc:lib","","");
  }catch(Exception e){System.out.println("数据库连接错误!"); }    
try{
  rs=con.createStatement().executeQuery(sqle);
   }catch(Exception e){System.out.print(e);}
return rs;

}

JSP页面:
 <s:iterator value="list"  id="list"  status="sta">
   
   <tr>
        <td>
        <s:property  value="lid"/>
        </td>
        <td>
        <s:property  value="zhuti"/>
        </td>
        <td>
        <s:property value="time"/>
        
        </td>
        <td>
        <s:url action="Serchl.action" id="url">
<s:param name="xhao">
<s:property value="lid" />
</s:param>
</s:url>
        <s:a href="%{url}">查看详情</s:a>
        
        </td>
   
   </tr>
   </s:iterator>
  






------ For reference only ----------------------------------- ----
you guys back too J8 crap right

you what the teacher does not teach you to resources after release ah

that do not get used up after CLOSE CONNECTION pure cheat points fraught ah !

------ For reference only -------------------------------- -------
+ +
------ For reference only -------------------------- -------------
a word , forward
------ For reference only ------------------ ---------------------
delivery really .
------ For reference only -------------------------------------- -
arrived Caicai I do not leave it blank hope that I will have problems you can help me you are cattle X
------ For reference only ----------------------------- ----------
Haha , 2nd Floor Shuafen come ~ ~ ~
------ For reference only ------------- --------------------------
me to brush strange . .
------ For reference only -------------------------------------- -
I am also a beginner , this card is also a long time, we were dizzy , then know that access to the home page of a web application is not necessarily accessible XX.jsp file , you can directly access control layer first servlet let it be to return home ( the other: this is the next change in the configuration file web.xml
For example, I :

PostsServlet
com.newpaging.controller.PostsServlet



PostsServlet
> / Posts


Posts

没有评论:

发表评论

<% = msgBoard.getAddresser ()%> <% = msgBoard.getContent ()%>