parkhill04
asked on
Where does PrintWriter object come from ?
When programming Java servlets, the interface HttpServletResponse extends the interface ServletRespose which has the method getWriter(). This method returns a PrintWriter object.
A typical code would be :
import java.io.*
import javax.servlet.*
import javax.servlet.http.*
public class ServNm extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("Hello World") ; } }
When was the PrintWriter object created ? There was no 'new PrintWriter' statement written.
And since java.io.* has been imported, why could you not use the code :
PrintWriter out = new PrintWriter;
out.println("Hello World"); ?
A typical code would be :
import java.io.*
import javax.servlet.*
import javax.servlet.http.*
public class ServNm extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("Hello World") ; } }
When was the PrintWriter object created ? There was no 'new PrintWriter' statement written.
And since java.io.* has been imported, why could you not use the code :
PrintWriter out = new PrintWriter;
out.println("Hello World"); ?
HttpResponse object has it's own PrintWriter object implementation. So to get that, we need to use response.getWriter()
component.addKeyListener(n ew MyKeyListener());
public class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt) {
// Check for key characters.
if (evt.getKeyChar() >= '0' && evt.getKeyChar() <='9' ) {
process(evt.getKeyChar(),v alue);
}
}
}
String strValue="";
public void process(char c, int code)
{
if(code<=9)
{
if(strValue.length()==1) return;
strValue=""+c;
}
else if(code<=99)
{
if(strValue.length()==2) return;
strValue=""+c;
}
}
strValue holds integer as String
using Integer.parseInt(strValue) u can get Integer
public class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt) {
// Check for key characters.
if (evt.getKeyChar() >= '0' && evt.getKeyChar() <='9' ) {
process(evt.getKeyChar(),v
}
}
}
String strValue="";
public void process(char c, int code)
{
if(code<=9)
{
if(strValue.length()==1) return;
strValue=""+c;
}
else if(code<=99)
{
if(strValue.length()==2) return;
strValue=""+c;
}
}
strValue holds integer as String
using Integer.parseInt(strValue)
sorry,
I posted the second comment accidentaly on ur link
Your answer is
HttpResponse object has it's own PrintWriter object implementation. So to get that, we need to use response.getWriter()
Now to ur other query
PrintWriter is a way to print content to destination device
So we need to bind destination device to PrintWriter object. it could be file or console or ur browser.
Response object has targeted destination device as browser.
I posted the second comment accidentaly on ur link
Your answer is
HttpResponse object has it's own PrintWriter object implementation. So to get that, we need to use response.getWriter()
Now to ur other query
PrintWriter is a way to print content to destination device
So we need to bind destination device to PrintWriter object. it could be file or console or ur browser.
Response object has targeted destination device as browser.
The PrintWriter instance is created by the servlet container for you.
ASKER
"HttpResponse has its own PrintWriter object implementation." - The HttpResponse (HttpServletResponse ?) can implement a PrintWriter object, but when does the PrintWriter object become an object ?
"The PrintWriter instance is created by the servlet container for you." - When does this happen, and by what means ? I know the servlet container creates an HttpServletResponse object and passes it on to the service method, but how does the PrintWriter object get created ? I had never heard of the servelt container creating a PrintWriter object automatically.
"The PrintWriter instance is created by the servlet container for you." - When does this happen, and by what means ? I know the servlet container creates an HttpServletResponse object and passes it on to the service method, but how does the PrintWriter object get created ? I had never heard of the servelt container creating a PrintWriter object automatically.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.