write to file from servlet

Ads
 

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.

Ads









Related Tutorials/Questions & Answers:
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
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
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 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
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
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
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 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
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
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
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
how to write servlet program in eclipse?
how to write servlet program in eclipse?  how to write servlet program in eclipse
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
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
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 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
calling servlet from jsp
calling servlet from jsp  how to call a servlet from jsp
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
calling servlet from JS and return response from servlet to JS
calling servlet from JS and return response from servlet to JS  hello...) which inturn calls a servlet(myServlet).servlet performs some DB related task.now i want this servlet(myServlet) to return a url(eg. pages/file.jsp
servlet file upload
servlet file upload  please tell me the complete code to upload a file on localhost using servlet
html and servlet file
in class folder..n WEB-INF. but..i want to access the servlet file through... html file with the jsp files amd servlet file insdie classes folder of tomcat. You... servlet from html like this: <form method="post" action="http://localhost
sending a zip file to servlet
sending a zip file to servlet  I have created a .zip file in a servlet on the local system(no static ip). The .zip file contains xml files. I have to send it to another servlet which is in a server(has a static ip). I have done
how to get a column values from a excel file after attaching it - JSP-Servlet
how to get a column values from a excel file after attaching it  hi sir, How to get a column values from a excel file after attaching it while sending a email,while sending a email if i attached a file that should be read
Upload csv or .xlsx file from JSP form to Database Table using servlet
Upload csv or .xlsx file from JSP form to Database Table using servlet  dear sir, i need the Servlet code that reads the .xlsx or CSV excel file... to Submit My Project i am using following code which is Working for .xls Excel file
The type Font is ambiguous where generating pdf file from jsp page - JSP-Servlet
The type Font is ambiguous where generating pdf file from jsp page  I am geeting ambiguous error .Do u want to add any jar files   Hi Friend, Import the class Font into your JSP page like the following
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

Ads