CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> General -> Web Design

 how to set the values from the database on the webpage using JSP and Servlets

Print topic Send  topic

Author Message
k_archana

Posts: 2
Posted: 01/11/2008, 3:28 AM

hai iam new to this topic plz help me..


i have a problem in setting the values of the database on the textfield present in the web page iam sending the code plesa help me..


JSP program (student.jsp) :


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student</title>
</head>
<body>
<form action="Student" method="post">
<h1>Student</h1>
StudentName : <input type="text" name= "stuname" value="" /><br>
StudentRoll : <input type="text" name= "sturoll" value="" /><br>
Student Marks1: <input type="text" name= "stumarks1" value="" /><br>
Student Marks2: <input type="text" name= "stumarks2" value="" /><br><br><br>
<input type="submit" name = "button" value="Save" />
<input type="submit" name = "button" value="Delete" />
<input type="submit" name = "button" value="Update" />
<input type="submit" name = "button" value="Find" />
</body>
</html>

------------------------------------------------------------------------------------------------
web.xml is :



<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">;

<servlet>
<servlet-name>Student_2</servlet-name>
<servlet-class>Student</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>Student_2</servlet-name>
<url-pattern>/Student</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
student.jsp

</welcome-file>
</welcome-file-list>
</web-app>


-------------------------------------------------------------------------------------------------

Student.java is




import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Student extends HttpServlet {

Connection con;
private String servname;
private int servroll;
private float servmarks1;
private float servmarks2;
private String servbutton;
private PreparedStatement pstsave;

private String servbutton1;

private String servbutton2;

private String servbutton3;

private PreparedStatement pstdel;

private PreparedStatement pstup;

private String servbutton4;

private PreparedStatement pstfind;

private String findbutton;

ResultSet rsfind;



protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();


try
{
System.out.println("entered try");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://Terragon20;databasename=Student1","sa","sa@1234");

servbutton = request.getParameter("button");
System.out.println("button =" + servbutton);


if(servbutton.equals("Save"))
{
System.out.println("entered save");
servname = request.getParameter("stuname");
System.out.println("the stuname ="+servname);
servroll = Integer.parseInt(request.getParameter("sturoll"));
System.out.println("the roll no" + servroll);
servmarks1 = Float.parseFloat(request.getParameter("stumarks1"));
System.out.println("the marks1 =" + servmarks1);
servmarks2 = Float.parseFloat(request.getParameter("stumarks2"));
System.out.println("marks2 =" + servmarks2);



pstsave = con.prepareStatement("insert into stu_details(sname,srollno,smarks1,smarks2)values(?,?,?,?)");

pstsave.setString(1,servname);
System.out.println("name");
pstsave.setInt(2,servroll);
System.out.println("roll");
pstsave.setFloat(3,servmarks1);
System.out.println("m1");
pstsave.setFloat(4,servmarks2);
System.out.println("m2");
pstsave.executeUpdate();

System.out.println("Save is success");

}

else if(servbutton.equals("Delete" ))
{

System.out.println("entered delete");

servroll = Integer.parseInt(request.getParameter("sturoll"));
System.out.println("the roll no" + servroll);

pstdel = con.prepareStatement("delete from stu_Details where srollno=?");

pstdel.setInt(1,servroll);
pstdel.executeUpdate();

System.out.println("delete is success");

}

else if(servbutton.equals("Update"))
{

servroll = Integer.parseInt(request.getParameter("sturoll"));
servname = request.getParameter("stuname");
servmarks1 = Float.parseFloat(request.getParameter("stumarks1"));
servmarks2 = Float.parseFloat(request.getParameter("stumarks2"));


pstup = con.prepareStatement("update stu_details set sname=?,srollno=?,smarks1=?,smarks2=? where srollno = ?");
pstup.setString(1,servname);
pstup.setInt(2,servroll);
pstup.setFloat(3,servmarks1);
pstup.setFloat(4,servmarks2);
pstup.setInt(5,servroll);
pstup.executeUpdate();

System.out.println("update is success");

}

if(servbutton.equals("Find"))
{


System.out.println("entered find");
servroll = Integer.parseInt(request.getParameter("sturoll"));
pstfind = con.prepareStatement("select sname,smarks1,smarks2 from stu_details

pstfind.setInt(1,servroll);

rsfind = pstfind.executeQuery();
while(rsfind.next())
{
// here iam confused and unable to write the code please help me...

}




}

}
catch(Exception e)
{
e.printStackTrace();
}


out.close();
}



protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}

}


--------------------------------------------------------------------------------

if servbutton = Find then i should get the student details from the database and should print in the text fields of the web page , iam not getting it please help me.....

_________________
archana
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.