Receive checkbox values in Java EE - java

I have this simple html form code:
<html>
<form method="post" action="http://localhost:7001/checkuser">
<input type="checkbox" name="name" value="1">John</input>
<input type="checkbox" name="name" value="2">Matt</input>
<input type="checkbox" name="name" value="3">Chris</input>
<input type="submit" name="sprawdz" value="Submit" />
</form>
</html>
And when i receive this post request in a node.js app with express I see that the posted object has a "name" array with the checked values in it.
When I try this in a Java EE servlet:
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class NewServlet extends HttpServlet {
private static final long serialVersionUID =1L;
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
}
The name variable only contains the first element checked, and I know how to get the whole array. I tried
String[] name = request.getParameter("name");
but this tells me that the left and right side are incompatible.

You can use getParameterValues() method..
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class NewServlet extends HttpServlet {
private static final long serialVersionUID =1L;
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String[] myarr = request.getParameterValues("name");
for(String s:myarr) {
out.println("Name : " + s);
}
}

Related

How can i get my table values from html to servlet?

I need to get my html table values in my servlet
Hi,
Im doing a project during my acadamic courses about sudoku website.
During my project I have encounterd a problem that I can't slove - get my table html values into my servlet.
I have tried doing things like set hidden names and getParameterValues but none of them worked.
this is my html table
<table class="center">
<% int n =9;
for(int s = 0; s<n; s++){
%>
<tr>
<% for(int f=0; f<n; f++)
{
%>
<td><% int z = SF[s][f];
if(z==0) {%>
<input type="text">
<% } else { %>
<%=SF[s][f]%>
<%}%>
</td hidden name="z">
<% } %>
</tr hidden name="z">
<% } %>
</table>
and this is my empty servlet
package View;
import org.omg.CORBA.SystemException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
#WebServlet(name = "CheckSudokuServlet",urlPatterns = "/CheckSudokuServlet")
public class CheckSudokuServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
//tried - String td[]=request.getParameterValues("z");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
How about using javascript to encode the values into a url
var z=3;
window.location.href = "/CheckSudokuServlet?z="+z;
Then in your servlet you can access as so:
String refBgcId= request.getParameter("refBgcId").toString();

passing parameters between jsp pages and servlets

I am new to web services jsps and servlets and i have this very simple example just to understand how things work.
At first, i have this simple web service :
package com.sav.calculator;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
#WebService(serviceName = "CalculatorWS")
public class CalculatorWS {
#WebMethod(operationName = "add")
public int add(#WebParam(name = "i") int i, #WebParam(name = "j") int j) {
int k = i + j;
return k;
}
}
Then i use this web service in my client application. Im trying to work the right way so i send data from a jsp to servlet, do the calculations in the servlet and send the data in another jsp for the presentation.. but the question is why im not getting it right?
here is the first jsp(just an html form):
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="POST" action="ClientServlet">
<input type="text" name="j"/>
<input type="text" name="i"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
here is the servlet where i use my add webmethod:
package com.sav.calculator.client;
import com.sav.calculator.CalculatorWS_Service;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
#WebServlet(name = "ClientServlet", urlPatterns = {"/ClientServlet"})
public class ClientServlet extends HttpServlet {
#WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/CalculatorWSApplication/CalculatorWS.wsdl")
private CalculatorWS_Service service;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
doPost(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
int i = (int) request.getAttribute("i");
int j = (int) request.getAttribute("j");
int k = add(i, j);
request.setAttribute("k",k);
RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("newjsp2.jsp");
dispatcher.forward(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}
private int add(int i, int j) {
com.sav.calculator.CalculatorWS port = service.getCalculatorWSPort();
return port.add(i, j);
}
}
And the newjsp2 is just a hello world page, im just trying to get there first but what i get is :
that.
After starting web-service server, in web browser type address:
http://localhost:8080/CalculatorWSApplication/CalculatorWS.wsdl
if this address contains an wsdl (xml format) then use it as your wsdlLocation.
Try also some tool, like SoapUI, or some other.
From Servlet to JSP
You could set values into the response object before forwarding request to jsp. Or you can put your values into a session bean and access it in the jsp.
From JSP to Servlet
You need to submit a form and pass parameter as an input. An example ...
<form method="Post" action="path/to/servlet">
<input type="text" name="x" />
<input type="password" name="xx" />
<input type="hidden" name="xxx" value="zzz" />
<input type='submit' />
</form>

unknown Servlet exception throw error of HTTP Status 500

I have tried adding all libraries that are needed for Servlet to run but still I am getting an exception that threw HTTP Status 500 error.
I also have tried all previously asked questions in stack overflow.
LoginServlet.java
package com.testlogin;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter pw = response.getWriter();
pw.print("Hello");
if (username.equals(password)) {
pw.println("<h1>Hello </h1>" + username);
}
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="LoginServlet" method="post">
<div style="color: red">
User Name
<input type="text" name="username">
<br /> Password
<input type="password" name="password">
<br>
<input type="submit" value="Sign In">
</div>
</form>
</body>
</html>
well, don't complicate your code. I think your code will work fine too and problem should be on your server.
please make sure if you are using servlet version >3.0 and tomcat 7 as it will not run in the previous versions of tomcat
try this..
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter pw = response.getWriter();
pw.print("Hello");
if (username.equals(password)) {
pw.println("<h1>Hello </h1>" + username);
}
}
}

Jetty on a non web project

I have a plugin project where I want to implement some servlets with jetyy. I have a login.html with this structure:
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>LOGIN.HTML</title>
</head>
<body>
<h1>LOGIN</h1>
<form method='POST' action=''>
User: <input type="text"> <br />
Password: <input type="password"> <br />
<input type="submit" value="login">
</form>
</body>
</html>
After I submit the form, I want to call a servlet to do some actions...
This is the servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/receptor")
public class HelloServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("TEST");
}
}
But I can't call it, I always have the 404 not found error, what's the right thing to write on the action of the form ?
PS: I've tried to create the WEB-INF structure and the web.xml, but I always got bad configuration error of this web.xml .. Is it possible to call the servlet without web.xml ?

why doesn't the servlet retrieve the part ? It shows null as the file name

The html snippet sends a post request to a servlet named servlet. The request is of type multipart/form-data.But servlet finds nothing and prints null for the name of the part I try to retrieve. Why is that ?
<form method="post" action="servlet" enctype="multipart/form-data">
<input type="file" value="browse" name="FileShared" />
<input type="submit" value="submit" />
</form>
import javax.servlet.http.Part;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
//String fileName = request.getPart("FileShared").getName();
// Throws a nullpointer exception if I don't comment the above statement
PrintWriter writer = response.getWriter();
//writer.println(fileName);
Collection<Part> c = request.getParts();
Iterator i = c.iterator();
while(i.hasNext()) {
writer.println("Inside while loop"); // This statement never gets printed
writer.println(i.next());
}
writer.println("outside while loop"); // Only this statement gets printed
}
If you want to use Servlet 3.0 HttpServletRequest#getParts() method , then you must annotate your servlet with #MultipartConfig.
Example :
#WebServlet(urlPatterns={"/SampleServlet"})
#MultipartConfig
public class SampleServlet extends HttpServlet {
}

Categories