getParameter returns null in the servlet [duplicate] - servlets

This question already has an answer here:
HTTP request parameters are not available by request.getAttribute()
(1 answer)
Closed 6 years ago.
i am trying to pass a string from my jsp to the servlet via href. I use this
Title1
then in the servlet i am trying to get the value of the select with this
String s = request.getParameter("select");
But it returns null. Why is that? Thank you!
EDIT I post the code of the servlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userOption = request.getAttribute("select");
System.out.println("Select is:" + userOption);
//in the console i get: Select is:null

That should not be String userOption = request.getAttribute("select");
Please understand that attributes are not parameters
It should be String userOption = request.getParameter("select");

Related

How to read the following path from a servlet? [duplicate]

This question already has answers here:
Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
(7 answers)
Closed 1 year ago.
I have a servlet which works on organizations address (#WebServlet("/organizations")). This way using GET or POST method on address .../organizations leads to calling of this servlet. When I need to work with a current organization (for example, 12th), I should call .../organizations/12. This way I can write #WebServlet("/organizations/*"), but how to read this number (12 in this case)? Or can I replace it with a variable somehow like #WebServlet("/organizations/{orgNumber}") (this variant didn't work)?
You did not give us your code, but you can use the request object and string operations to find the part of the request URI you are looking for.
#WebServlet("/organizations/*")
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
// split the complete URI by /
final var split = request.getRequestURI().split("/");
// ...
// or use substrings
final var partOfPath = request.getRequestURI().substring(20,30);
// ...
// or use pathInfo to split only the path following the domain
final var split = request.getPathInfo().split("/");
// ...
}
}
You could map it on /organizations/* and extract information from getPathInfo():
#WebServlet("/organizations/*")
public class OrganizationsController extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] pathInfo = request.getPathInfo().split("/");
String id = pathInfo[1]; // {id}
String command = pathInfo[2];
// ...
//..
//.
}
}

Servlets, How to make server available by the name in annotation? [duplicate]

This question already has an answer here:
How to set my webapp to appear as ROOTfor localhost:8080
(1 answer)
Closed 1 year ago.
I'm only starting to learn Servlets.
I have a simple template code below.
I thought that after writing annotation "#WebServlet("/hello-servlet")" my page will be available by the URL "http://localhost:8080/hello-servlet".
The problem is that it is not available by that adress only by "http://localhost:8080/demo_war_exploded/".
I do know that the problem is connected with Tomcat configurations. In settings it is said that url is "http://localhost:8080/demo_war_exploded". And deployment is "war exploded".
How can I make the server available by the name in annotation?
#WebServlet("/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
I found out the answer - you just need to delete application context from deployment

Servlet 404 the requested resource [/ServletDemo/AddServlet] is not available [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 2 years ago.
I just started learning about Servlets. I followed the basic steps to create a new servlet project:
File -> new -> Dynamic Web Project -> new -> Servlet (AddServlet) with a basic print statement in the doGet()
package com.demo.servlets;
//all of my imports
#WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AddServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().print("Hello");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
I'll I've done is make the servlet but I can't get past this step. I've looked at a million tutorials and stack overflow answers. I made sure Tomcat 9.0 was installed and connected to my project correctly, made sure the servlet version was 4.0, made sure my build path was correct, made sure that my servlet was in a package.
I've also even tried using web.xml instead of the web annotations but I get the same 404error. . Ive been working on this for the past 72 hours, and I don't understand what the problem is.
You need to override doGet and doPost from HttpServlet. For this reason you need to add #Override annotation above the doGet and doPost.

how to get a path variable value in servlet

I want to get the path variable in servlet. Assume the url is www.demo.com/123/demo. I want to get the 123 value from the path without doing any string manipulation operation.
Note: the following servlet doesn't have any web.xml configurations. My code is:
#WebServlet(urlPatterns = { "/demo" })
public class DemoServlet extends HttpServlet {
public DemoServlet()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
sysout("demo");
}
}
The portion of the URL you are referring to is the "context". Use request.getContextPath() to get this. In the case of your example, this would return /123. If you want exactly 123 you would have to remove the leading slash.
From the documentation:
Returns the portion of the request URI that indicates the context of
the request. The context path always comes first in a request URI. The
path starts with a "/" character but does not end with a "/"
character. For servlets in the default (root) context, this method
returns "". The container does not decode this string.

Servlets: doPost and doGet simple inquiry [duplicate]

This question already has answers here:
doGet and doPost in Servlets
(5 answers)
Closed 6 years ago.
public class CornelltaxiServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//resp.setContentType("text/plain");
//resp.getWriter().println("Hello, world");
}
}
From my understanding of doGet and doPost, it shouldn't matter where I put the "Hello, world" message. However, when I try to print it using the doPost method, it does not work. Could anyone explain this for me?
Also, from
void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Preprocess request: we actually don't need to do any business stuff, so just display JSP.
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
}
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
what does this do?
The doGet() method is called when HTTP GET is requested (e.g. when you type your servlets' URL in the browser). Then Hello, world will appear in the browser.
doPost() on the other hand will be used on HTTP POST. You need for example:
<form method="POST" action="/your/servlet"
When you submit such a form, you should see "Hello, world" (that is - when you uncomment it) in the browser as well.
As for your second question:
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
This will forward request processing to hello.jsp. Basically, the contents of that file will be rendered instead of your Hello, world. Sending both content using resp.getWriter() and forwarding is a mistake. Pick one.

Resources