Couldn't get the values from jsp to servlet - java

If I run jsp, while exporting the contents to excel, I am not getting the values in downloaded excel file. It is simply empty.
Here what I tried..
How to pass the table values to servlet?
Excel.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import ="javax.swing.JOptionPane"%>
<!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>Export to Excel - Demo</title>
</head>
<body>
<table align="left" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
<p>
fsfndfkdsfdkfjsfksfskfsfskfsfksfskf
dfkdjfkfksfkkkkkkkkkjjjjjjjjj
</p>
Export to Excel
</body>
</html>
Sample.java (Servlet)
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Sample
*/
public class Sample extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Sample() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String exportToExcel = request.getParameter("exportToExcel");
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "excel.xls");
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

Try this:
<%# page language="java" contentType="application/vnd.ms-excel"
pageEncoding="ISO-8859-1"%>
<%# page import ="javax.swing.JOptionPane"%>
<!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>Export to Excel - Demo</title>
</head>
<body>
<table align="left" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
<p>
fsfndfkdsfdkfjsfksfskfsfskfsfksfskf
dfkdjfkfksfkkkkkkkkkjjjjjjjjj
</p>
Export to Excel

Related

How to redirect jsp

I have LoginServlet like this:
package servlets;
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("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("gogikole") && password.equals("1234")) {
response.sendRedirect("mainMenu.jsp");
return;
}
}
}
And login.jsp like this:
<%# 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>Login page</title>
</head>
<body>
<form method="post" action="LoginServlet"></form>
<table>
<tr>
<td>User name</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</body>
</html>
For some reason when I input correct username: (gogikole) and password: (1234) and click on submit button it won't to redirect me to mainMenu.jsp
When I click on button, nothing happen it just stay on login page like I didn't even click or type anything.
How to fix that problem and redirect when I click on submit button (Login)?
I created mainMenu.jsp and, for now that is one allmost blank page only with message "Welcome".
Your submit button is not submitting the form.
i.e., you have closed the form immediately:
<form method="post" action="LoginServlet"></form>
The problem is that your form inputs and submit button are outside the form You have to enclose as below.
<form method="post" action="LoginServlet">
<table>
<tr>
<td>User name</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>

Can't call doPost() method of servlet in jsp page [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
At beggining. There is a application structure.
I have 3 MySQL tables ( Obiekt, Termin, Rezerwacja). In project i have jsp page (here is code) :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# page import="java.sql.*"%>
<%# page import="java.io.*"%>
<%# page import="java.util.*"%>
<%# page import="test.Obiekt"%>
<%# page import="test.ListaObiektow"%>
<%# page import="test.Termin"%>
<%# page import="test.ListaTerminow"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>menu główne</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<meta name="android-mobile-web-app-capable" content="yes">
<meta name="android-mobile-web-app-status-bar-style" content="black">
<link href="css/ratchet.css" rel="stylesheet">
<link href="css/ratchet-theme-android.css" rel="stylesheet">
<script src="js/ratchet.js"></script>
<script type="text/javascript">
function Refresh(idObiekt){
location.href="pilkaNozna.jsp?idObiekt=" + idObiekt;
}
</script>
</head>
<body>
</br>
</br>
</br>
<header class="bar bar-nav">
<a class="icon icon-left-nav pull-left" href="wyszukaj.jsp"></a>
<h1 class="title">Wybierz obiekt</h1>
</header>
<div id="content">
<div class="tabelawybor">
<b>Wybierz obiekt:</b>
<%
ArrayList<Obiekt> list = new ListaObiektow().getObiekty();
%>
<form>
<select name="obiekt" onChange="Refresh(this.value)">
<option value="0" selected>Wybierz Obiekt</option>
<%
String selectedObiekt = request.getParameter("idObiekt");
int counter=0;
for (Obiekt obiekt : list) {
if(selectedObiekt == null && counter==0)
{
selectedObiekt = Integer.toString(obiekt.idObiekt);
}
%>
<option value="<%=obiekt.idObiekt%>"
<%= ((Integer.toString(obiekt.idObiekt)).equals(selectedObiekt))?"selected":""%>><%=obiekt.nazwa%>
<%=obiekt.adres%></option>
<%
}
%>
</select>
</form>
</div>
<div class="tabelawybor">
<td><b>Wpisz liczbę uczestników:</b><input type="text"
name="uczest" /></td>
<% String liczbaUczestnikow = request.getParameter("liczbaUczestnikow"); %>
</div>
<div class="tabelawybor">
<form action="Rezerwacja?action=doPost" method="post">
<table class="center">
<tr>
<td>Nazwa obiektu:</td>
<td>Data:</td>
<td>Godzina</br> rozpoczęcia:
</td>
<td>Godzina</br> zakończenia:
</td>
<td></td>
</tr>
<%
ListaTerminow listaterminow = new ListaTerminow();
listaterminow.setId(selectedObiekt);
ArrayList<Termin> lista =listaterminow.getTerminy();
String idTermin = request.getParameter("idTermin");
for (Termin termin : lista) {
%>
<tr>
<td><%=termin.nazwaObiektu%> <%=termin.adresObiektu%></td>
<td><%=termin.dzien%></td>
<td><%=termin.odKtorej%></td>
<td><%=termin.doKtorej%></td>
<td>
<button class="btn btn-primary">Zarezerwuj</button>
</td>
</tr>
<%
}
%>
</table>
</form>
</div>
</div>
</body>
</html>
In this jsp page i have select form which have List of Obiekt (this list is created in ListaObiektow.java), but it works. I also have table form which contains List of Termin (created in ListaTerminow.java). It works too. But, in this table form there is button to make Rezerwacja.
I made Rezerwuj servlet. Here is code:
package test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
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 test.ConnectionClass;
import test.Rezerwacja;
import test.Termin;/**
* Servlet implementation class Rezerwuj
*/
#WebServlet("/Rezerwacja")
public class Rezerwuj extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection conn;
private int idTermin;
private int liczbaUczestnikow;
public Rezerwuj() {
super();
}
/**
* #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 {
conn = ConnectionClass.Polacz();
ArrayList<Rezerwacja> rezerwacje = new ArrayList<Rezerwacja>();
PreparedStatement st = null;
ResultSet rs = null;
String sql = "INSERT INTO rezerwacje (liczbaUczestnikow,idTermin) values ('" + liczbaUczestnikow + "','" + idTermin + "')"
+ "UPDATE termin SET termin.czyZajety=true WHERE termin.idTermin = '"+ idTermin +"'";
try
{
st = conn.prepareStatement(sql);
if(liczbaUczestnikow > 0 && liczbaUczestnikow < 20)
{
rs = st.executeQuery();
}
while(rs.next())
{
Rezerwacja rezerwacja = new Rezerwacja();
rezerwacja.setLiczbaUczestnikow(rs.getInt(1));
rezerwacja.setIdTermin(rs.getInt(2));
rezerwacje.add(rezerwacja);
}
}
catch(SQLException e)
{
System.out.println(e);
}
}
public void setIdTermin(String id)
{
idTermin = Integer.parseInt(id);
}
public void setliczbaUczestnikow(String liczba)
{
liczbaUczestnikow = Integer.parseInt(liczba);
}
}
When i click on the button, it returns error like this:
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
test.Rezerwuj.doPost(Rezerwuj.java:63)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
What i want:
I select one of option in select form, then type value (0-20) into text input, then click on button and create a record in MySQL table Rezerwacja. Any suggestions ?
<form action="Rezerwacja?action=doPost" method="post">
In this line the action is where you are sending the request, so it should be action="Rezerwacja" and method should be method="POST"
I think you have problem with liczbaUczestnikow after initialization is always 0. As result rs always null, therefore exception thrown.

How to get the table content value in java?

Here, I am getting table content value (datatoexport) in servlet. But unable to get in excel page.
Getting empty page when download. Here, I am exporting table contents to excel in java.
Your help is highly appreciated.
JSP Code:
<%# 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>Export to Excel - Demo</title>
<script src="scripts.js"></script>
<script language="javascript">
function exportToExcel()
{
alert("test");
$("#datatoexport").val( $("<div>").append( $("#exportTableSelector").eq(0).clone() ).html() );
$('#myForm').submit();
}
</script>
</head>
<body>
<form id="myForm" action="Sample" method="post">
<table id="exportTableSelector" align="left" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
<br><br>
<p>
some text
</p>
<input type="hidden" name="datatoexport" id="datatoexport" value="">
<input type="hidden" value="excel_download" name="fileName" id="fileName">
<a href="" onclick="exportToExcel();" target="_blank" >Export to Excel</a>
</form>
</body>
</html>
Servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Sample
*/
public class Sample extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Sample() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Inside doGet");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Inside doPost");
actionExportToExcel(request, response);
}
public void actionExportToExcel(HttpServletRequest request, HttpServletResponse response)
{
String fileName = request.getParameter("fileName");
System.out.println("filename: " +fileName);
String datatoexport = request.getParameter("datatoexport");
System.out.println("datatoexport: " +datatoexport);
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=test_file.xls");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0);
System.out.println("datatoexport" +datatoexport);
}
}
Doesn't look like you are writing any data to the response. Try adding
response.getWriter().write(datatoexport);
response.getWriter().flush();
response.getWriter().close();
First get the PrintWriter object from HttpServletResponse by
PrintWriter out=response.getWriter();
then try writing whatever you want to write like
out.println("Whatever you want to write.");
You can do it client side, without a server request by doing something like this:
function exportToExcel()
{
var datatoexport = $("<div>").append( $("#exportTableSelector").eq(0).clone() ).html();
window.open('data:vnd.ms-excel;charset=utf-8,' + escape(datatoexport));
}

JSP org.eclipse.persistence.platform.database does not exist error

I am getting the following trace
[#|2014-04-01T14:56:57.824+0200|WARNING|glassfish3.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=215;_ThreadName=Thread-1;|StandardWrapperValve[ServletExample]:
PWC1406: Servlet.service() for servlet ServletExample threw exception
org.apache.jasper.JasperException: PWC6033: Error in Javac compilation
for JSP
PWC6199: Generated servlet error: package
org.eclipse.persistence.platform.database does not exist
when clicking on the Submit Button
<form action="servletexample" method="post">
<table border="0">
<tr>
<td>Vorname</td>
<td><input type="text" name="vorname" /></td>
</tr>
<tr>
<td>Nachname</td>
<td><input type="text" name="nachname" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
This is my JSP class:
package com.example.tutorial;
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("/servletexample")
public class ServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("vorname") == null
|| request.getParameter("nachname") == null) {
getServletContext().getRequestDispatcher("/index.jsp").forward(
request, response);
return;
}
String vorname = request.getParameter("vorname");
String nachname = request.getParameter("nachname");
request.setAttribute("vorname", vorname);
request.setAttribute("nachname", nachname);
getServletContext().getRequestDispatcher("/output.jsp").forward(
request, response);
}
}
I am using Glasfish version 3.
Could anyone give me a quick hint?
EDIT: Added the output.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%#page import="org.eclipse.persistence.platform.database.FirebirdPlatform"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h1>Your first and last name is:</h1>
<%
String vorname = (String)request.getAttribute("vorname");
String nachname = (String)request.getAttribute("nachname");
out.print(vorname+" "+nachname);
%>
</body>
</html>
It failed due to this Eclipse Generated import, which was in my output.jsp
<%#page import="org.eclipse.persistence.platform.database.FirebirdPlatform"%>
removing it fixed the problem.

How to set values for hidden variable using javascript when processing using servlet? [duplicate]

This question already has an answer here:
HTTP request parameters are not available by request.getAttribute()
(1 answer)
Closed 6 years ago.
Hi all this is how i set value is hidden variable through java script,
function logintosystem(){
document.forms["frmLogon"].funtiontype.value="logon";
document.forms["frmLogon"].submit();
}
and this is what is my jsp page :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%
String path = "", userName = "", message = "", userType = "";
path = request.getContextPath();
if(request.getAttribute("message")!=null) message =(String)request.getAttribute("message");
%>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="ACCT"/>
<meta name="keywords" content="acct,accesscardcomparision" />
<meta name="author" content="slingmedia" />
<link rel="stylesheet" type="text/css" href="acct.css" title="andreas09" media="screen,projection" />
<title>ACCT</title>
<script>
function logintosystem(){
document.forms["frmLogon"].funtiontype.value="logon";
document.forms["frmLogon"].submit();
}
</script>
</head>
<body>
<div id="container">
<div id="sitename">
<h1>ACCT</h1>
<h2>Access Card data comparision with Leave portal</h2>
</div>
<div id="mainmenu">
<ul>
<%
if (userName.length() > 0 || !userName.equals(null) || !userName.equals("")) {
%>
<li><a href="#" class="current" >Welcome <%=userName%></a></li>
<%}%>
<%
if (userName.length() > 0 && userType.length() > 0) {
%>
<li>Tempcard</li>
<%}%>
<%
if (userType.equals("HR")) {
%>
<li>Report</li>
<%}%>
<%
if (userName.length() > 0 && userType.length() > 0) {
%>
<li>Logout</li>
<%}%>
</ul>
</div>
<div id="wrap">
<div id="content">
<h1>Please Enter your code</h1>
<form name="frmLogon" id="frmLogon" action="LogonServlet" method="post">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr align="center" valign="middle">
<td width="100%" align="center" valign="middle">
<table width="100%" align="left" border="0" cellspacing="0" cellpadding="2" >
<tr>
<td width="5%" align="left"> </td>
<td width="35%" align="right">ACCT Code</td>
<td width="20%"><input type="text" name="acctcode" id="acctcode" class="inputBoxes" /></td>
<td width="15%" align="left"><input type="button" class="submitButton" value="Logon" onclick="logintosystem();"/></td>
<td width="15%"> </td>
<td width="10%" align="center"> </td>
</tr>
<br></br>
</table>
</td>
</tr>
</table>
<input type="hidden" name="funtiontype" id="funtiontype" value=""/>
</form>
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFF0">
<tr>
<td id="message" align="center">
<b><font color="brown" size="3"><%=message%></font></b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div class="clearingdiv"> </div>
</div>
</div>
<div id="footer">
<p>© 2012 slingmedia | ACCT </p>
</div>
</body>
</html>
and my servlet is :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.slingmeadia.notifier.servlet;
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;
/**
*
* #author anthony.savarimut
*/
public class LogonServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet LogonServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet LogonServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
String funtiontype = "",acctcode="",pageRedirect="";
if(request.getAttribute("funtiontype")!=null) funtiontype =(String)request.getAttribute("funtiontype");
if(request.getAttribute("acctcode")!=null) acctcode =(String)request.getAttribute("acctcode");
if(funtiontype.equals("logon")){
request.setAttribute("message","Loggedon code is "+acctcode);
pageRedirect="notifier/notifier.jsp";
}else{
request.setAttribute("message","loggedout code is "+acctcode);
pageRedirect="index.jsp";
}
response.sendRedirect(pageRedirect);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
System.out.println("Called logon");
doGet(request,response);
}
/**
* Returns a short description of the servlet.
*
* #return a String containing servlet description
*/
#Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
the page is getting submited but the values that i set for hidden variable is not coming in servlet also i have a text box that value also is not coming to servlet when submitting the page.
Kindly help me to find out this.
You should do this:
funtiontype =request.getParameter("funtiontype");
acctcode = request.getParameter("acctcode");
Try this..
function logintosystem(){
document.frmLogon.funtiontype.value="logon";
document.frmLogon.submit();
}
Update:
String funtionType = request.getAttribute("funtiontype") == null ? request.getParameter("funtiontype") : request.getAttribute("funtiontype");
String acctCode = request.getAttribute("acctcode") == null ? request.getParameter("acctcode") : request.getAttribute("acctcode");

Categories