Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet write to file from servlet
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


miki
write to file from servlet
4 Answer(s)      5 years and 10 months ago
Posted in : JSP-Servlet



Advertisement
View Answers

January 12, 2009 at 1:47 AM


Hi friend,


I am sending adding, deleting and updating code. Please implement following code.


<html>

<title>User information</title>
<head>

<script type="text/javascript">

function validateForm(theForm){



if(theForm.name.value==""){

//Please enter username

alert("Please enter User Name.");

theForm.name.focus();

return false;

}
if(theForm.lsname.value==""){
//please enter passward
alert("Please enter Last Name.");
theForm.lsname.focus();
return false;
}

return true;
}
</script>

</head>



<body >

<br><br><br>

<div align="center">

<center>

<table border="1" cellpadding="0" cellspacing="0" width="400px" height="69" bgcolor="pink">

<tr>

<td width="859" height="69">

<form method="GET" action="Insertdata" onsubmit="return validateForm(this);">



<h1 align="center">&nbsp;User Information Form</h1>

<div align="left">

<table border="1" cellpadding="0" cellspacing="0" width="400px">



<tr>

<td width="50%"><b>First Name:</b></td>

<td width="50%"><input type="text" name="name" size="20"></td>

</tr>

<tr>

<td width="50%"><b>Last Name:</b></td>



<td width="50%"><input type="text" name="lsname" size="30">

</td>

</tr>



</table>

</div>

<p align="center"><input type="submit" value="Submit" name="submit">

<input type="reset" value="Reset" name="B2"></p>

</form>

<a href="ShowDataAction"><b>Click for show data</b></a>

</td>

</tr>

</table>

</center>

</div>



</body>



</html>


January 12, 2009 at 1:49 AM


ShowDataAction file


package javacode;

import java.io.*;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowDataAction extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName ="amar";
String password="amar123";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,userName,password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select id,name,lsname from userinform");
out.println("<html><head><title>show data</title></head>");
out.println("<body>");
out.println("<br><br><br>");
out.println("<center>");
out.println("<h2>Display data from Database<h2>");
out.println("</center>");
out.println("<center>");
out.println("<table border=1 width=400px cellspacing=0 cellpadding=0>");
out.println("<tr>");
out.println("<td><b>Id<b></td>");
out.println("<td><b>Name<b></td>");
out.println("<td><b>Last Name<b></td>");
out.println("<td><b>Delete<b></td>");
out.println("<td><b>Edit<b></td>");
out.println("</tr>");
int sno=0;
while(rs.next()){

int id = rs.getInt("id");
String name = rs.getString("name");
String lsname = rs.getString("lsname");
String action = "DeletedataServlet/"+id;
String action1 = "EditdataServlet/"+id;
out.println("<tr>");
out.println("<td>");
out.println(++sno);
out.println("</td>");
out.println("<td>");
out.println(name);
out.println("</td>");
out.println("<td>");
out.println(lsname);
out.println("</td>");
out.println("<td>");
out.println("<a href="+action+">Delete</a>");
out.println("</td>");
out.print ("<td><a href=Updaterecord.jsp?id="+id+">Edit</a></td>");

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

}
out.println("</table>");
out.println("</body></html>");
}
catch (Exception e){
out.println(e);
}
}
}


January 12, 2009 at 1:50 AM


Delete action file:


package javacode;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;

public class DeletedataServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName ="amar";
String password="amar123";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//String id=request.getParameter("id");

System.out.println( "uri : "+ request.getRequestURI());
String strurl = request.getRequestURI().toString();
int strIndex = strurl.lastIndexOf("/");
String strId = strurl.substring(strIndex+1);
int id = Integer.parseInt(strId);
System.out.println("strId" + strId);
System.out.println("id:"+id);
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,userName,password);

try{
Statement st = con.createStatement();
String sqlQuery ="delete from userinform where id="+id;
int delete = st.executeUpdate(sqlQuery);
con.close();
out.println("Successfully Delete data into Database");
}

catch(SQLException ex){
System.out.println("SQL satatment not found");
}
}
catch(Exception e){
e.printStackTrace();
}
}

}


January 12, 2009 at 1:52 AM


edit action file:


package javacode;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;

public class EditdataServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName ="amar";
String password="amar123";
response.setContentType("text/html");
PrintWriter out = response.getWriter();

System.out.println( "uri : "+ request.getRequestURI());
int id = Integer.parseInt(request.getParameter("id"));
System.out.println("id:"+id);
String name = request.getParameter("name");
String lsname = request.getParameter("lsname");
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,userName,password);

try{
Statement st = con.createStatement();
String sqlQuery ="update userinform set name='"+name+"',lsname='"+lsname+"' where id="+id;
System.out.println("sqlQuery "+ sqlQuery);

int delete = st.executeUpdate(sqlQuery);
con.close();
out.println("Successfully Delete data into Database");
}

catch(SQLException ex){
System.out.println("SQL satatment not found");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}

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

Visit for more information:

http://www.roseindia.net/servlets/

Thanks.


Related Tutorials/Questions & Answers:
write to file from servlet - JSP-Servlet
write to file from servlet  Hi, I have a jsp file where I input data and retrive data through servlet. However; when I edit data it is not showing right data on the web site. I am also trying to write this data to file
how to write file from FileInputStream
how to write file from FileInputStream  Hi, How to Write a file.... Thanks,   Hi, For Write to file from FileInputStream in java you may use... this website Write to file from FileInputStream in java
Advertisements
how to write to file from string in Java
how to write to file from string in Java  Hi, Please any one help me for how to write to file from string in Java. I am beginner in Java programming. Thanks,   Hi, Are you eager to learn how to write to file from
Java Write To File From FileInputStream
Java Write To File From FileInputStream In this tutorial you will learn how to write to file from FileInputStream. Write to file from FileInputStream in java... input bytes from a file or can say it reads the streams of bytes. And to write
Java Write To File From String
Java Write To File From String In this tutorial you will learn how to write to file from string. Write to file from string using java at first we will have... you how to write a file from string"; File file = null
How to write .properties file from jsp
How to write .properties file from jsp  Hi i new to java i stuck here please help me... my problem is "I wrote a class to write the .properties file... to implement/write .properties file from jsp. Regards, Venkatesh Gurram
How to write file from inputstream in java
How to write file from inputstream in java  Hi, how to write file from inputstream in java program. Plz suggest an example or suggested link... link to your query "How to Write to inputStream in Java". This is good
upload a file and write it in JSP using servlet
upload a file and write it in JSP using servlet  Hello, I'm facing a problem here. I want to upload a file through abc.jsp and write the contents of file using a servlet in xyz.jsp. It is supposed to be a excel file which
write xml file with jsp useBean - JSP-Servlet
write xml file with jsp useBean  how to write into xml files with jsp.. the code for writing is in a class.. pls help me to solve this..thanx... an org.w3c.dom.Document from XML. Save the xml file at bin file of C:\apache-tomat-5.5.23\bin
without writing web.xml file we can write servlet program
without writing web.xml file we can write servlet program  Sir Morning... Sir I have one Question "without writing web.xml file we can write servlet program". if yes which way? if no why? but without use Annotation........ Plz
How to Retrieve Data from the database and write into excel file using Java
How to Retrieve Data from the database and write into excel file using Java  Hi, I am trying to develop an small application where i trying to retrieve Data from the database and store the details in excel file. Please can
Retrieve data from the database and write into ppt file
Java Retrieve data from the database & write into ppt file In this section, we are going to retrieve data from the database and write into the .ppt file. For this purpose, we have used Jakarta POI api to create a .ppt file. The class
Retrieve Data from the database and write into excel file
Retrieve Data from the database and write into excel file. In this section, we are going to retrieve data from the database and write into the excel file... = connection.createStatement(); ResultSet rs = st.executeQuery("Select * from student
Uploading file in servlet from a html form
Uploading file in servlet from a html form  Sir, I want to upload a picture from my html file and save it to my database as BLOB,but what JAR should i use for this purpose i am really confused about.And also is it possible to do
Read and write file
Read and write file  HI, How to read and write file from Java program? Thanks   Hi, See the following tutorials: Java Write To File Read File Thanks
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload   Dear Sir, My Question is: How display a image on servlet from file upload Your Answer: Hi... of file. like any .jpg, .txt. Servlet page dispaly empty in each cases
Read Text file from Javascript - JSP-Servlet
Read Text file from Javascript  plz send the code How to Retrieve the data from .txt file thru Javascript? And how to find the perticular words in that file
How display a Image on Servlet from file upload - JSP-Servlet
How display a Image on Servlet from file upload  Dear Sir, My requirement is I want to display a Image on Servlet from File Upload. But It not display on servlet,Its appear a downloaded form and downloaded on disk when click
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java. Please suggest and give example of this program. thanks
retrieving of value from excel file - JSP-Servlet
this message to all the employees when i upload a file then it fetch a data from that excel sheet i.e this matter will take a para meter values from the excel...retrieving of value from excel file  Dear sir, Thanks for sending
retrieving of value from excel file - JSP-Servlet
this message to all the employees when i upload a file then it fetch a data from that excel sheet i.e this matter will take a para meter values from the excel...retrieving of value from excel file  Dear sir, Thanks for sending
How to Write to file in Java?
How to Write to file in Java?  How to Write to file in Java Program?   Hi, For writing a file in Java, we need some classes from Java.IO... the Examples of How to write to File in Java Program: WriteToFileExample.java import
Write data in doc file
Write data in doc file  How to write data or string in word file
How to Write to a File in Java
will be able to use OutputStreamWriter on a FileOutputStream classes to write data to a file from Java program. To write the Java Write To File we will use two...In this Java tutorial, we will demonstrate how to write to a file in Java
Getting Parameter from a css styled jsp file to a java servlet file...
Getting Parameter from a css styled jsp file to a java servlet file... ...; java servlet file-- /* * To change this template, choose Tools... is it not getting the parameter??? How do i accept the value in the servlet file
how to upload a file of apk format in a folder from servlet
how to upload a file of apk format in a folder from servlet  How to upload a file of apk format to a folder from servlet, i am trying to do, its getting uploaded as .zip file and not apk file
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload  Dear Sir, My issue is: How display a image on servlet from file upload I receive your answer today....jar and set that on classpath.I keep the servlet file in javacode package. First
How display a Image on servlet from file upload - JSP-Servlet
How display a Image on servlet from file upload  Dear Sir, I were ask a question that How display the Image on servlet through file upload. Today I get your answer. But Sir, It code not display the image on servlet
How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java... bufferedWriter object to write in the file. For more details and examples related to How to write in File of Java Program
Java write to file
Java write to file  How to write to a file in Java? Is there any... files. You can easily use the the FileWriter and BufferedWriter to write data to a text file. Here is the examples: Example program to write to file. File
retrieving of value from excel file - JSP-Servlet
retrieving of value from excel file  Dear sir, Thanks for sending a code now i am getting a particular column value i.e EmailId column for snding a massmails,now i have to get a particular column values i.e Name(A),EL(B
Advertisements
 

 

 

DMCA.com