• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in JSP/Servlets

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic