Hello friends,
I want some help. If anybody can help me out for my problem. I am new to JSP. I am using NetBeans6.0 IDE , MySQL server for the database. I have differentiated my work between servlets,Javabeans and JSP. The logic of my project is stored in the servlet and JSP is used for presentation purpose only. Scriplets are not allowed for my project. I am having the text fields and dropdown object in my JSP page. I am able to retrieve the data written in the textfields but I am not able to retrieve the value selected in the dropdown.
In my one JSP page there are textfields and a dropdown. And I want that the details filled in the textfield and the value selected in the dropdown should be displayed on the next JSP page when I click on the button. The data written in the textfield are displayed but I am not able to get the data of dropdown on the other page. I have tried with the code "request.getParameter("name of the combo");" but I am not getting any value using this code. My code is like this.........
My bean is FormBean.java
package Bean;
import javax.servlet.http.*;
/**
*
* @author Administrator
*/
public class FormBean {
private String name;
private String mname;
private String lname;
private String email;
private String address;
private String cr;
public FormBean()
{
name="";
mname="";
lname="";
email="";
address="";
cr="";
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email=email;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address=address;
}
public String getCr()
{
return cr;
}
public void setCr(String cr)
{
this.cr = cr;
}
}
My servlet is SubmitHandler.java
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SubmitHandler extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
Vector errors=new Vector();
String name=request.getParameter("name");
String mname=request.getParameter("mname");
String lname=request.getParameter("lname");
String address=request.getParameter("address");
String email=request.getParameter("email");
String cr=request.getParameter("Countryresidence");
if(! isValidEmail(email))
{
errors.add("Email Address should be of type asdfsf@yahoo.com");
}
if(name=="")
{
errors.add("First Name field is compulsory");
}
if(mname=="")
{
errors.add("Second Name field is compulsory");
}
if(lname=="")
{
errors.add("Last Name field is compulsory");
}
if(email=="")
{
errors.add("Email field is compulsory");
}
if(address=="")
{
errors.add("Address field is compulsory");
}
if(cr=="")
{
errors.add("Country Residence should be selected");
}
String next;
if(errors.size() == 0)
{
next="/Form.jsp";
}
else
{
String[] errorArray=(String[])errors.toArray(new String[0]);
request.setAttribute("errors",errorArray);
next="/Submit.jsp";
}
RequestDispatcher rd;
rd=getServletContext().getRequestDispatcher(next);
rd.forward(request,response);
}
private boolean isValidEmail(String email)
{
boolean gotat=false;
boolean gotdot=false;
char nextchar=' ';
boolean em = false;
do
{
for(int i=0;i<email.length()-1;i++)
{
nextchar= email.charAt(i);
if(nextchar=='@')
{
gotat=true;
}
if(nextchar=='.' && gotat==true)
{
gotdot=true;
}
}
}while(nextchar==' ' && nextchar=='\n');
if(gotat==true && gotdot==true)
{
em=Boolean.parseBoolean(email);
em=true;
return em;
}
else
{
em=false;
return em;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
doGet(request,response);
}
My JSP page is Submit.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="Submit" class="Bean.FormBean">
<jsp:setProperty name="Submit" property="*"/>
</jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Submit Page</title>
</head>
<body background="D:\background.jpg">
<h2>Submit Resume</h2>
<%
String[] errors = (String[])request.getAttribute("errors");
if(errors!=null && errors.length >0)
{
%>
<b>Please correct your errors</b>
<ul>
<% for(int i=0; i<errors.length; i++) {%>
<li><%=errors[i]%>
<%}%>
</ul>
<%}%>
<form action="SubmitHandler" method="post">
<table align="left">
<table style="border-collapse:collapse;" border="1" bordercolor="tan" cellpadding="0" cellspacing="0" width="100%">
<td>
<table align="centre" border="0" cellpadding="2" cellspacing="1" width="100%">
<tbody>
<tr bgcolor="tan">
<td class="points" colspan="3">
<b> Personal Details</b>
</td>
</tr>
<tr>
<td>
<strong>First Name:</strong>
<span class = "asterisk" ID="FName">*</span>
</td>
<td>
<input type="text" name="name" value="<jsp:getProperty name="Submit" property="name"/>">
</td>
</tr>
<tr>
<td>
<strong>Middle Name:</strong>
<span class = "asterisk" ID="MName">*</span>
</td>
<td>
<input type="text" name="mname" value="<jsp:getProperty name="Submit" property="mname"/>">
</td>
</tr>
<tr>
<td>
<strong>Last Name:</strong>
<span class = "asterisk" ID="LName">*</span>
</td>
<td>
<input type="text" name="lname" value="<jsp:getProperty name="Submit" property="lname"/>">
</td>
</tr>
<tr>
<td>
<strong>Address:</strong>
<span class = "asterisk" ID="Address">*</span>
</td>
<td>
<input type="text area" name="address" value="<jsp:getProperty name="Submit" property="address"/>">
</td>
</tr>
<tr>
<td>
<strong>E-mail Address:</strong>
<span class = "asterisk" ID="email">*</span>
</td>
<td>
<input type="text" name="email" value="<jsp:getProperty name="Submit" property="email"/>">
</td>
</tr>
<tr>
<td>
<strong> Country Residence:</strong>
<span class = "asterisk" ID="countryres">*</span>
</td>
<td width="69%">
<SELECT NAME="country" ID="Countryresidence">
<OPTION VALUE="Please select" selected>-Please Select-</OPTION>
<OPTION VALUE="<jsp:getProperty name="Submit" property="cr"/>">Australia</OPTION>
<OPTION VALUE="India">India</OPTION>
<OPTION VALUE="Other">Other... </OPTION>
</SELECT>
</td>
</tr>
</table>
</table>
<table cellSpacing=1 cellPadding=1 width="100%" align=center border=0>
<tbody>
<tr>
<td align="center">
<input type="submit" value=" Submit " name="Submit">
</td>
</tr>
</tbody>
</table>
</body>
</html>
My Form.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="Submit" class="Bean.FormBean">
<jsp:setProperty name="Submit" property="*"/>
</jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<H1>Please check the details filled by you in the previous form</H1>
<H2>Click the submit button if the details are perfect. Otherwise fill the form again</H2>
<table align="left">
<table style="border-collapse:collapse;" border="1" bordercolor="tan" cellpadding="0" cellspacing="0" width="100%">
<td>
<table align="centre" border="0" cellpadding="2" cellspacing="1" width="100%">
<tbody>
<tr bgcolor="tan">
<td class="points" colspan="3">
<b> Personal Details</b>
</td>
</tr>
<ul>
<b>First Name:</b> <jsp:getProperty name="Submit" property="name"/><br>
<b>Middle Name:</b> <jsp:getProperty name="Submit" property="mname"/><br>
<b>Last Name:</b> <jsp:getProperty name="Submit" property="lname"/><br>
<b>Address:</b> <jsp:getProperty name="Submit" property="address"/><br>
<b>Email:</b> <jsp:getProperty name="Submit" property="email"/><br>
<b>Country Residence :</b> <jsp:getProperty name="Submit" property="cr"/><br>
<b>Country Residence change:</b>
</ul>
</table>
</table>
<table cellSpacing=1 cellPadding=1 width="100%" align=center border=0>
<tbody>
<tr>
<td align="center">
<input type="submit" value=" Submit " name="Submit">
</td>
</tr>
</tbody>
</table>
</body>
</html>
The contents filled in the Submit.jsp should be displayed on the Form.jsp. The details filled in the textboxes are displayed in the Form.jsp but the value selected in the combobox is not displayed. I am not able to find the solution. Please help me as early as possible.
I am also facing problem in retrieving the data from the database. This is my new project.I want to display the data from database on the JSP page using Servlets and javabeans. Please help me out for this also.
This is my index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="index" class="CB.ControlBean" >
<jsp:setProperty name="index" property="*"/>
</jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hello World!</h2>
<form action="IndexHandler" method="post">
<table>
<tr>
<td>Username: </td>
<td>
<input type="text" name="username" value="<jsp:getProperty name="index" property="username"/>">
</td>
</tr>
<!--<tr>
<td>Password: </td>
<td>
<input type="password" name="password" value="">
</td>
</tr>-->
<tr>
<td>
<input type="submit" name="submit" value=" Submit ">
</td>
</tr>
</table>
</form>
</body>
</html>
My servlet and bean are as follows
ControlBean.java
package CB;
/**
*
* @author Administrator
*/
public class ControlBean {
private String username;
//private String password;
public ControlBean()
{
username="";
// password="";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
My servlet IndexHandler.java
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author Administrator
*/
public class IndexHandler extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
String username=request.getParameter("username");
// String password=request.getParameter("password");
Connection conn=null;
int a =0;
try
{
if(username != null)
{
// Class.forName("org.gjt.mm.mysql.Driver");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","krishna");
Statement stat=conn.createStatement();
//String query ="insert into UserInfo values( "+"'"+username+"',"+"'"+password+"' )";
String query ="select * from UserInfo where Username="+"'"+username+"'";
ResultSet results=stat.executeQuery(query);
//System.out.println("<h2>"+"You have successfully entered data"+"</h2>");
System.out.println(results);
//out.println("<h3>"+"<a href="+"http://localhost:8080//JspApplicationProject//view.jsp"+" target=/"+"_parent/"+">UpdateData<//a>"+"</h3>");
}
}
catch(ClassNotFoundException cnf)
{
System.out.println("cnf"+cnf.getMessage());
System.out.println(cnf);
}
catch(SQLException exp)
{
System.out.println("exp"+exp.getMessage());
System.out.println(exp);
}
finally
{
try
{
if(conn!= null)
conn.close();
}
catch(SQLException esp)
{
System.out.println(esp.getMessage());
}
}
System.out.close();
}
I am, trying that the result set should be displayed on next JSP page.
Please help me as early as possible. Please...............
Thanx in advance.