wood burning stoves 2.0*
The moose likes JSP and the fly likes Need help in JSP/Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Need help in JSP/Servlets" Watch "Need help in JSP/Servlets" New topic
Author

Need help in JSP/Servlets

Sakthi Ramasamy
Greenhorn

Joined: Jun 02, 2008
Posts: 17
Hi,

My requirement is a simple web project using JSP/Servlets/JDBC.

I need to display the username and comments in the same page of the input form with the previous entries in database.

I handled it in separate pages but now i need to display it in one jsp page.

please help me out in this.

Thanking you all in advance.

-
Sakthi.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

Sakthi Ramasamy wrote:I handled it in separate pages but now i need to display it in one jsp page.

Where have you stuck displaying in the same JSP?
Sakthi Ramasamy
Greenhorn

Joined: Jun 02, 2008
Posts: 17
Kindly have a look at my code....

GuestServlet.java

package com.msat.cloud;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class GuestBookServlet
*/
public class GuestBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public GuestBookServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

PrintWriter out = response.getWriter();

String name = request.getParameter("name");
String email = request.getParameter("email");
String comments = request.getParameter("comments");

int rowCount = 1;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cloudmsat?user=root&password=welcome");
Statement stmt = con.createStatement();

stmt.executeUpdate("insert into guestbook3 values('" + name
+ "','" + email + "','" + comments + "');");


ResultSet rs = stmt.executeQuery("select * from guestbook3");

ResultSetMetaData resultsMetaData = rs.getMetaData();

int columnCount = resultsMetaData.getColumnCount();

out.println("<html><table width=\"100%\" datapagesize=\"80%\" border=\"1\" >");
out.println("<TR color=\"Gray\"><TD bgcolor=\"Gray\">Name</TD><TD bgcolor=\"Gray\">EMail</TD><TD bgcolor=\"Gray\">Comments</TD></TR>");
ArrayList all=new ArrayList();
while (rs.next()) {
rowCount++;
out.println("<tr border=\"1\">");

for (int i = 1; i <= columnCount; i++) {
out.println("<td>"+ rs.getString(i)+"</td>");
all.add(rs.getString(i));
request.setAttribute("all", all);
}

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

}
System.out.println(all);
out.println("</table></html>");
rs.close();
stmt.close();
con.close();


} catch (Exception e) {
e.printStackTrace();
}
if (name== null || email== null || comments == null){
out.println("The guestbook cannot be signed if you have left any of the fields blank");
}
out.println("you are the guest No: "+ rowCount);
}
}



MyJSP Page


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<table>
<tr>
<jsp:useBean id="list" class="java.util.ArrayList" scope="session"/>
<jsp:useBean id="it" class="java.util.Iterator" scope="session"/>
<%String book=request.getParameter("all");%>
</tr>
</table>


<form method="post" action="GuestBookServlet" >
<%! private int accessCount = 0; %>
<!--<H5>Page Hits: -->
<!--<%= ++accessCount %></H5>-->
<table>
<tr><td>Name:</td> <td><input type="text" name="name" size="20" maxlength="50"></td></tr>
<tr><td>Email:</td> <td><input type="text" name="email" size="20" maxlength="50"></td></tr>
<tr><td><br>Your comments:</td><td><br><textarea name="comments" cols="30" rows="5"></textarea></td></tr>
<tr><td></td><td><br></br><input type="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>



I donot know how to post it in the same page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
Sakthi please don’t post the same message in multiple forum. you are wasting people time . Please post message in appropriate forum.

http://www.coderanch.com/t/476683/Servlets/java/help-GuestBook-Application
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

Sakthi Ramasamy wrote:I donot know how to post it in the same page.

Check for a boolean flag or something (which is set after checking whether user has valid entries in DB) then display the data where you want them in the JSP.
Sakthi Ramasamy
Greenhorn

Joined: Jun 02, 2008
Posts: 17
Hi,

The reason for duplicate post is the thought that my post might not get noticed. but i clicked the resolved option as i didn't know how to delete the post.

I am [b]extremely sorry[/b] for the people, whose time i have wasted and caused some irritation.

[u]I thank you very much for the ideas that you have provided for me to solve the issue[/u].

I assure that the next time i post some issues, i will follow the rules.

Best Regards,
Sakthi.
 
wood burning stoves
 
subject: Need help in JSP/Servlets