write to file from servlet

write to file from servlet

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
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
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
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.ADS
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
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...;This example demonstrates you how to write a file from string"; File file
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
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
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
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
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
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
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
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
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
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 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 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
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
not able to get values from jsp file - JSP-Servlet
, her am sending the file jsp and servlet but in the servlet file am not able to get... remove encrtype from form tag its work fine please go through the file...; ================================================================= servlet file
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
Write data in doc file
Write data in doc file  How to write data or string in word file
how to write servlet program in eclipse?
how to write servlet program in eclipse?  how to write servlet program in eclipse
how to generate the pdf file with scroolbar from jsp age - JSP-Servlet
how to generate the pdf file with scroolbar from jsp age  How to generate the pdf file with scroolbar from jsp.i am not able to see the all the columns in pdf file now .it is very urgent for me plz help
callig jsp file from javascript function - JSP-Servlet
callig jsp file from javascript function  I want to call a jsp file from javascript function. What I want to do is Whenever a user clicks... data, no button and no links. Is it possible to call jsp page from static html
File download from server to client machine - JSP-Servlet
File download from server to client machine  hi, I want to save a pdf file to my client machine from a button provoded in a jsp page.The file that was generated by jasper libraries.In my code i can give path for save
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
How to Write to a File in Java
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... and execute it, this will write "Hello Java" into out.txt file. While creating
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
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
'Hello World' file from a servlet (PDF, HTML or RTF).
 'Hello World' file from a servlet (PDF, HTML or RTF...; In this program we are going to tell you how we can create three file rtf,pdf and html...*,import com.lowagie.text.html.*; ADS_TO_REPLACE_1 Now create a file named
Write records into text file from database
Write records into text file from database You have already learnt how to insert records from text file to database. But here we are going to retrieve records from database and store the data into the text file. For this purpose, we have
Redirect from servlet to servlet
Redirect from servlet to servlet  I want to insert data from Text box... to redirectServlet , and from this servlet I want to again forward my page to insertServlet(i.e...-name> <welcome-file-list> <welcome-file>index.html</welcome
Calling servlet from servlet .
Calling servlet from servlet .  How to call a servlet from another servlet in Java?   You can call another servlet by using... ServletException, IOException { System.out.println("Calling another servlet by using
pdf file is having the 0 KB when generate the pdf file from jsp - JSP-Servlet
pdf file is having the 0 KB when generate the pdf file from jsp  Hi Friend, I am getting OKB pdf file .if i inserting the image into pdf file , i am getting the OKB pdf file now and also not able to open the pdf
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
Download file - JSP-Servlet
Servlet download file from server  I am looking for a Servlet download file example
How to write to file using FileOutputStream
How to write to file using FileOutputStream  Hi friends, Please help me in java program. How to write to file using FileOutputStream? thanks,   Hi, To write a file using FileOutputStream, we have to use
How to Write To File BufferedWriter in Java
How to Write To File BufferedWriter in Java  how to write to file... in Java.io package i.e. FileWriter and BufferedWriter file. In this section we... to write texts. In this classwe stores the character in a buffer to write
how to write to file at the end of the line
how to write to file at the end of the line  Hi, Where do i learn how to write to file at the end of the line using Java program. Plz suggest... will help you for "How to write the file at the end of line using Java language
How to write file by line in Java
How to write file by line in Java  How to write file by line in Java   Hi, For Write to a file by line using java programming language we... in a new line. How to use Write To File By Line in Java programs
CREATE AND WRITE FILE THREAD JAVA
CREATE AND WRITE FILE THREAD JAVA  Hi guys I was wondering how can I make this program in java with threads. I need to create a file and write... a beginner :(   It has to listen from any port **port=6001 (example) Filesize
How to write into CSV file in Java
How to write into CSV file in Java  How to write into CSV file in Java   Hi, To create Comma separated value(CSV) file is very simple... passed the file name to write into a stream characters. Then we use the FileWriter
calling servlet from jsp
calling servlet from jsp  how to call a servlet from jsp

Ads