Hi please some one help me in this issue.
I have a class which extends HttpServlet and a method called doGet(HttpServletRequest request)
But i dont have a HttpServletResponse .Now i want to respond to a request from a client as im not having response how can i respond to a request.Can i create able to create it dynamically in that class using reflections and is their any other way..My problems pseudo code looks as follows
protected void doGet(HttpServletRequest request)throws ServletException, IOException {//having only request as parameter in doGet()
//Want to respond to a jsp with out having a response
//how can i create a response object if i got a HttpServletResponse response the code looks as follows
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");
if(name.equals("James")&& password.equals("abc")){
response.sendRedirect("result.jsp");
}
else{
pw.println("u r not a valid user");
}
}
There is no such method protected void doGet(HttpServletRequest request) throws ServletException, IOException
The method signature is:
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
Related
I'd like to modify the response object of type HttpServletResponse, I've implemented the filter which extends OncePerrequestFilter base class.
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
os = response.getOutputStream();
}
I'd like to get the response as String, modifying it and updating the HttpServletRequest instance content with my modified String. It is possible ?
The form calls the servlet logout.
<form name="fm1" method="post" action="logout">
This is the servlet code:
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.getRequestDispatcher("index.jsp").include(request, response);
HttpSession session=request.getSession();
session.invalidate();
out.print("You are successfully logged out!");
out.close();
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("index.jsp").include(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
session.invalidate();
out.print("You are successfully logged out!");
out.close();
}
I am unable to understand how to get to the servlet to execute using doGet when I'm using post in my form. How will it be called?
Call doGet(request, response) method in post method of servlet
This probably is not an answer, and I am unable to comment, but why are you not using the doPost method?
Or change the method in the form to get?
When I use request.getRequestDispatcher(myurl).forward(request, response) in servlet to jump to another HTML page, I find it doesn't work. A response can be seen in web browser but the page just doesn't change.
I tried response.sendRedirect(myurl) then, and it also doesn't work.
Here is my code:
public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) {
System.out.println("get");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.sendRedirect("/ForwardTest/new.html");
// request.getRequestDispatcher("/new.html").forward(request, response);
System.out.println("success");
}
}
Each time I run this, just get a "success" output in console. That means getRequestDispatcher had run, but not correctly.
I am new in Servlets. I am trying to map a URL using the wildcard (*), but it's not working the way i expected.
Here is my servlet class.
#WebServlet(urlPatterns = {"/A/*"})
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Working...");
}
}
The above servlet is working for both example.com/A and example.com/A/car.
I want to work the servlet only for the the second option which is example.com/A/whatEver. How can i do that ?
In simple: I just want to work the servlet if there's anything after example.com/A.
Any help will greatly appreciated.
Just invoke HttpServletResponse#sendError() with SC_NOT_FOUND (404) when HttpServletRequest#getPathInfo() is null or empty or equals to /.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// ... (continue)
}
Generally we specify the doGet and doPost in our HTML code, so that servlet will invoke these methods with respect to the call in HTML code.
Is there any way that whether we call doPost or doGet Servlet doPost method will get called?
I know there is one way that in doGet we can call doPost method , But apart from that is there any other method .
Calling one from the other, or better - calling a 3rd method from both is the best approach.
You can override the service() method as well, but there's more code there that you might not want to loose.
You can create a common method and put it into doGet() or doPost().
protected void processRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}