Home Answers Viewqa JSP-Servlet write to file from servlet

 
 


miki
write to file from servlet
4 Answer(s)      4 years and 5 months ago
Posted in : JSP-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 Pages:
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
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
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
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 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
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 .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
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
can we write a method in JSP - JSP-Servlet
can we write a method in JSP  Hi All, In my web application I want to call another second jsp file. I can do it by redirecting my first jsp file to second jsp file, but I can't come back to the same place(instruction
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
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
JavaScript write to text file
a TextStream object to read from or write to the file. The Boolean value defined... JavaScript write to text file  ... are going to create a file and write text into it using JavaScript. In the given example
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
Java file read write operation
Java file read write operation  how to read and write the data from text file.Suppose i have text file with 4 fields name ,roll no ,marks1,marks2 with more than 20 records......i need to store these value in object and pass
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
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
Java Write To File BufferedWriter
Java Write To File BufferedWriter In this tutorial you will learn how to write...;This example demonstrates you how to write in a file using BufferedWriter."... to write into the file by java program. Download Source Code
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
Write to a file
Write to a file   ...; the BufferedOutputStream class that store the data in an internal buffer and lets you write...; to the existed file. The given example uses the BufferedOutputstream
Download file - JSP-Servlet
Servlet download file from server  I am looking for a Servlet download file example
How to write a file in Java?
How to write a file in Java? To write a file in Java use the class FileWriter... or False. BufferedWriter: The BufferWriter class is used to write data from... and strings. Example of how to write text to a file in java: import java.io.
Java Write To File - Java Tutorial
Java Write To File - Java Tutorial Learn how to write to a file from... on a FileOutputStream classes to write data to a file from Java program... of Java Tutorial you will learn how to write java program to write to a file. We
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 write build file for one project - Ant
how to write build file for one project   hi This is kishore, i want to know how to write build file for one sample project in java. if u... are providing some links to you from where you will get to know creating a build.xml file
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
'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.*; Now create a file named helloServletPDF
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
file
file  Could anyone please help me to write a code that does the following: Opens a file named MyName.txt, reads the first line from the file and displays it, and then closes the file. Thank you so much
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write... through the above file and save it in maps depending on if it is a concept
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write... through the above file and save it in maps depending on if it is a concept
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write... through the above file and save it in maps depending on if it is a concept
Write date servlet and test on tomcat
Display the current date with a Servlet       This servlet program is going to show you how...; It is very easy to display current date with the help of a servlet program using
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
Applet Write Files Example
;  In this section, you will learn how to write to a file from... the permission for writing a file from an applet. For this, you will have to maintain... a file from an applet. Without maintaining the Java security policy file
How to write content of one file to another file.
; In this example we will open a file and then write the content in new file... the use of CheckedOutputStream class. The CheckedOutputStream class is from... of the data being written in the second file. About CheckedOutputStream class
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
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
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, 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
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
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
is there any possibelities fast read and write file large data file
is there any possibelities fast read and write file large data file  ...) { //read from start original file String arry[] = sCurrentLine.split(delimeter); // it take row by row from original file
is there any possibelities fast read and write file large data file
is there any possibelities fast read and write file large data file  ...) { //read from start original file String arry[] = sCurrentLine.split(delimeter); // it take row by row from original file
is there any possibelities fast read and write file large data file
is there any possibelities fast read and write file large data file  ...) { //read from start original file String arry[] = sCurrentLine.split(delimeter); // it take row by row from original file

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.