Forums Register Login

Need help in JSP/Servlets

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Sakthi please don’t post the same message in multiple forum. you are wasting people time . Please post message in appropriate forum.

https://coderanch.com/t/476683/Servlets/java/help-GuestBook-Application
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
bacon. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2531 times.
Similar Threads
Creating a table in a JSP
XML on JSP
Storing and retrieving images
Please wait Screen...... required using JSP and Javascript
User Registration/Login and Hibernate
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:34:41.