Jetty on a non web project - java

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 ?

Related

Why am I getting null value while using "sendReditect" in servlet as per below

Why am I getting null value while using "sendReditect" in servlet as per below
my code as per below : I am getting fname value null even in both FirstServlet and SecondServlet
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstServlet" method="get">
username<input type="text" name="fname"></br> <input type="submit"
value="SUBMIT">
</form>
</body>
</html>
FirstServlet:
package com.naveen;
import java.io.IOException;
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;
/**
* Servlet implementation class FirstServlet
*/
#WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
String s3=request.getParameter("fname");
// TODO Auto-generated method stub
/*String s1=request.getParameter("t1");*/
/*RequestDispatcher rd=request.getRequestDispatcher("SecondServlet");
rd.forward(request, response);*/
res.sendRedirect("SecondServlet");
System.out.println("your output as per" +s3);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
SecondServlet:
package com.naveen;
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;
/**
* Servlet implementation class SecondServlet
*/
#WebServlet("/SecondServlet")
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String s3=request.getParameter("fname");
out.print("hi i am siddharth");
out.println(s3);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
why i am getting null value while using "sendReditect" in servlet as
per below my code as per below : i am getting fname value null even in
both FirstServlet and SecondServlet,
Because you're not setting any values to your request. You need to set the value to the request like this:
#WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
String s3=request.getParameter("fname"); //get the value you set in your jsp/html/url
request.setAttribute("fname", s3); // set the s3 value to the request
res.sendRedirect("SecondServlet");
System.out.println("your output as per" +s3);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
I'm assuming here you have sent the fname value via a form or something. If you call FirstServlet by just typing it in the url, you will get null.
But not if you set something, try it like this if you are not submitting a form:
/FirstServlet?fname=helloworld
EDIT:
just noticed in your form you're not actually setting fname with any value. You need to give it a value:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstServlet" method="get">
username<input type="text" name="fname" value="helloworld"></br> //add value to your input!!
<input type="submit" value="SUBMIT">
</form>
</body>
</html>

Receive checkbox values in Java EE

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);
}
}

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);
}
}
}

Form submit not working in servlet application

Below are the web.xml, servlet and jsp code
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello "+userName+" how are you ?");
}
}
JSPCode:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is First Servlet
<form action="firstTest">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td> Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input name = "sumbit "type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Web.xml Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Trial</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/firstTest</url-pattern>
</servlet-mapping>
</web-app>
when i give URL http:localhost:8080/Trial the Index.jsp is coming but when i gave username and password the URL is chaning to http:localhost:8080/firstTest instead of http:localhost:8080/Trial/firstTest and i am getting 405 error "Tomcat error HTTP Status 405 - HTTP method GET is not supported by this URL" is anything wrong in my code
typo...
protected void doGost
change to doPost or doGet...
In your jsp code form tag you have not specified method name, and by default it use GET method, so just replace this code, I hope it will work :
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello " + userName + " how are you ?");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
For to resolve this type of typo errors before we deploy into the server
We use #override annotation for to test whether the method is overloaded method or not. When ever you compiled the java it will throw exception. Actually compiler will check for syntactic errors etc. But in this scenario compiler treated doGost() is also a method by using above annotation at the compilation level only developer can understand where the problem occurred.
So. My suggition is #override for overriding methods.

Categories