Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using beans in JSP  RSS feed

 
sumana ar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i'm a learner of JavaServer Pages and i faced this problem..please condiser this as urgent...

i'm trying out a simple program for printing the names from the emp table.
i've written the program in java using oracle thin drivers, which works fine..but now i tried to use the same program as jsp useBean to print the names using jsp, which is showing an error...
can someone please write a simple java bean(for database connection) as well as jsp program to display the names of all the employees from emp table of test database..
thanks in advance..
sumana
 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) ServletBusca.java
...
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String fieldBusca = request.getParameter("fieldBusca");
String resultPage = "resultados_busca.jsp";
try {
BuscaBean buscaResult = new BuscaBean();
buscaResult.select(fieldBusca);
request.setAttribute("buscaResult", buscaResult);
RequestDispatcher dispatch = request.getRequestDispatcher(resultPage);
dispatch.forward(request, response);
}
...
2) BuscaBean.java
...
public void select(String param) throws Exception {
Connection con = conexao();
Statement stmt = null;
ResultSet rs = null;
param.replace('\'', ' ');
String select = "SELECT * FROM BUSCA WHERE TITULO LIKE '%"
+ param + "%' OR LINK LIKE '%"
+ param + "%' OR DESCRICAO LIKE '%"
+ param + "%' OR KEYWORDS LIKE '%"
+ param + "%'";
stmt = con.createStatement();
rs = stmt.executeQuery(select);
while (rs.next()) {

String row[] = new String[4];

resultados.addElement(row);

row[0] = rs.getString("titulo");
row[1] = rs.getString("link");
row[2] = rs.getString("descricao");
row[3] = rs.getString("keywords");

}
stmt.close();
con.close();
}
...
3) resultados_busca.jsp
...
<jsp:useBean id="buscaResult" scope="request" type="BuscaBean"/>
<%
int totalRegistros = buscaResult.getResultados().size();
String[] registroEncontrado;
%>
...
<%= totalRegistros %>
...
<%
if (totalRegistros > 0) {
try {
for(int i=0; ; i++) {
registroEncontrado = (String[])buscaResult.getResultados().elementAt(i);
%>
...
<%= i+1 %>. <a href="<%= registroEncontrado[1] %>"><%= registroEncontrado[0] %></a>
<%= registroEncontrado[2] %>
<%
}
} catch (Exception erro) {
}
}
%>
...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!