I have some code. It works in JSp but not in Java servlet. I am pasting my code here. Can u plz state the mistake in servlet file?
JSP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String rollno=null;
String category=null;
rollno=request.getParameter("rollno");
category=request.getParameter("category");
out.println(rollno+"\n"+category+"\n");
%>
</body>
</html>
Java Servlet :-
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;
public class a extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
String rollno=null;
String category=null;
rollno=request.getParameter("rollno");
category=request.getParameter("category");
out.println(rollno+"\n"+category+"\n");
}
}
Use doGet method not doPost
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
Related
I am working on a student management project in which I created two servlets in same folder, LoginController for handling the login and TestContoller for registering new student. The problem is that when I try to login LoginController is accessible and I am able to login but when i try to register a student through a register.jsp page mapped to TestController , I get HTTP Status 404 – Not Found. The requested resource [/StudentManagement/TestController] is not available error
Here is my code:-
register.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/TestController" method="post">
<input type="text" name="name">
<input type="submit" value="submit">
</form>
</body>
</html>
TestController
package com.controller;
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;
/**
* Servlet implementation class TestController
*/
#WebServlet("/TestController")
public class TestController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public TestController() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("name"));
}
}
I am having when using apache tomcat with java. The problem might be in the web.xml but I dont know how to fix it. This is my project explorer:
So the thing is that when I run registro.html everything goes as expected:
But when I click on enviar the 404 appears:
So this is my code in registro.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registro</title>
</head>
<body>
<form action="/Ejer2/Registro" method="POST">
<input type=hidden name=registro value=resultadoRegistro>
<BR><BR>Username: <input type=text name=user>
<BR><BR>Password: <input type=password name=pass>
<BR><BR><input type=submit value="Enviar"><input type=reset>
</form>
</body>
</html>
And this is my Registro.java:
package Ejer2;
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;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
#SuppressWarnings("deprecation")
#WebServlet(urlPatterns="/Registro")
public class Registro extends HttpServlet implements SingleThreadModel{
private static final long serialVersionUID = 1L;
public Registro() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session=req.getSession(true);
Usuario miuser=(Usuario)session.getValue(session.getId());
if(miuser==null){
miuser=new Usuario(req.getParameter("user"),req.getParameter("password"));
session.putValue(session.getId(),miuser);
}
res.setContentType("text/html");
String user=req.getParameter("user");
//String pass = req.getParameter("pass");
PrintWriter toClient = res.getWriter();
toClient.println("<html>");
toClient.println("<title>REGISTRO REALIZADO</title>");
toClient.println("Usuario "+user+" registrado con exito");
toClient.println("</html>");
toClient.close();
}
}
And this my web.xml:
I don't know what to do, thanks in advance.
Hi I am new to java coding, i trying to create dynamic web page in eclipse. this is my demoServlet.java code
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;
#WebServlet("/demoServlet")
public class demoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public demoServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out= response.getWriter();
out.println("<!DOCTYPE html PUBLIC \''>");
out.println("<html>");
out.println("<head>");
out.println("<script language=\'JavaScript\'>");
RequestDispatcher rd= request.getRequestDispatcher("/WEB-INF/javascript/Validate.js");
rd.include(request, response);
out.println("</script>");
out.println("</head>");
out.println("<body>");
out.println("<form action=\'/myWebprj/Success.jsp onSubmit=\'return Validateusername(this.username.value)\'>");
out.println("UserName: <input type=\'text\' name=\'username\'>");
out.println("<input type=\'submit\' value=\'Submit\'>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
this is my JSP page
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My First jsp page in servlets</title>
</head>
<body>
Its Successfully completed
</body>
</html>
and this is my javascript page
function Validateusername(username)
{
var returnValue=true;
if(username=""){
alert("Enter username is empty");
returnValue=false;
}
return returnValue;
}
when i am trying to enter SUBMIT button its showing me the error
HTTP Status 404 - /myWebprj/Success.jsp%20onSubmit=
--------------------------------------------------------------------------------
type Status report
message /myWebprj/Success.jsp%20onSubmit=
description The requested resource is not available.
Can any one help me out . whats is the error in the code...?
Might it be possible that you are somehow accessing your demoServlet servlet using POST instead of GET? This would explain your error message. Please check your JavaScript console to make certain it is using GET.
Also, please modify your doPost() method to the following:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
If this resolves the error, then we have the problem solved.
This works for me! I only have removed the return keyword from the onsubmit event attribute. I also have modified the code a bit to properly assign the username to the javascript function.
package test;
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;
#WebServlet("/demoServlet")
public class demoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public demoServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String username = request.getParameter("username");
out.println("<!DOCTYPE html PUBLIC \''>");
out.println("<html>");
out.println("<head>");
out.println("<script language=\'JavaScript\'>");
RequestDispatcher rd= request.getRequestDispatcher("/WEB-INF/javascript/Validate.js");
rd.include(request, response);
out.println("</script>");
out.println("</head>");
out.println("<body>");
/*
onSubmit should call only 'Validateusername()'
so the the 'return' keyword has been removed
*/
out.println("<form action=\'/Test/Success.jsp\' onSubmit=\'Validateusername(" + username + ")\'>");
out.println("UserName: <input type=\'text\' name=\'username\'>");
out.println("<input type=\'submit\' value=\'Submit\'>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
function Validateusername(username)
{
var returnValue=true;
if(username == undefined || username == null || username == ""){
alert("Enter username is empty");
returnValue=false;
}
return returnValue;
}
Your servlet code looks fine. The only change you need to make is in Validate.js file.
When validating username for empty you have used "=":
if(username=""){ // your code }
Instead you use "==", it works fine.
if(username==""){ // your code }
i have a simple jsp page with one anchor tag which will call the servlet page:
The following is the jsp code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Download Data</title>
</head>
<body>
View data in following format:<br>
MS-Excel
</body>
</html>
This is my servlet page :
package com.primeki.devlopment.usm.view;
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("/ExcelServlet")
public class Filedownload extends HttpServlet {
private static final long serialVersionUID = 1L;
public Filedownload() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/vnd.ms-excel");
PrintWriter out = response.getWriter();
out.println("Name\tJob\tSalary");
out.println("Raj\tAccountant\t20000");
out.println("Vinay\tAccountant\t20000");
out.println("Rajesh\tAccountant\t20000");
out.println("\tTotal:\t=sum(c2:c3)");
out.close();
}
}
I am getting an error when i am clicking the anchor tag ...
i want to make an excel sheet to get download by click on the anchor tag .. but i am getting an error ... plz help in this....
I may be mistaken because I am new in Java EE but it seems that MS-Excel anchor is redirecting to Filedownload while your servlet is #WebServlet("/ExcelServlet").
Try changing your anchor to MS-Excel
You have named your WebServlet "/ExcelServlet" which means that it weill respond on requests made to http://{server}/{app-name}/ExcelServlet and you have a link with href = "Filedownload"
I stumbled upon servlets and I just love them compared to scriptlets since they perfectly divide logic and view. But I'm having trouble calling instance methods in my JSP page.
I have the following JSP page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${stringarray}">
${stringarray}
<br/>
</c:forEach>
</body>
</html>
And the following Servlet:
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Servlet
*/
#WebServlet("/Servlet")
public class Servlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public Servlet()
{
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String[] strarray = new String[5];
strarray[0] = "zero";
strarray[1] = "one";
strarray[2] = "two";
strarray[3] = "three";
strarray[4] = "four";
request.setAttribute("stringarray", strarray);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
}
}
Why can't I call the arrays methods with the dot separator in my JSP page?!
I think what you're looking for is the following:
<c:forEach var="stringElement" items="${stringarray}">
${stringElement}
<br/>
</c:forEach>
The c:forEach tag loops over each element in the ${stringarray}, but to access each item, you have to define a variable. See also the TLD docs