I've created a jsp login page form and I've put my logic inside the doPost(). But seems it is not working.
Even the System.out.println statement result in doPost() method is not shown in the console.
JSP page::(LogIn.jsp)
<body>
<form method="post" action="LogIn">
<table>
<tr>
<td colspan=2 align="center"
style="font-weight: bold; font-size: 20pt;" align="center"><b>User
Login Form</b></td>
</tr>
<tr>
<td colspan=2></td>
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" id="txtUserName" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" id="txtpassword" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" id="btnSubmit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
Servlet class (LogIn.java)
#WebServlet("/LogIn")
public class LogIn extends HttpServlet {
private static final long serialVersionUID = 1L;
public LogIn() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
String UserName = request.getParameter("txtUserName");
String Password = request.getParameter("txtpassword");
System.out.println(UserName);
System.out.println(Password);
if (UserName == "Servlet" && Password == "admin") {
response.sendRedirect("Home.jsp");
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
// Read single valued data
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
{
System.out.println("no values");
}
else
System.out.println(paramValue);
} else {
// Read multiple valued data
System.out.println(".....");
for (int i = 0; i < paramValues.length; i++) {
System.out.println( paramValues[i]);
}
}
}
}
}
Web.xml file
<servlet>
<servlet-name>LogIn</servlet-name>
<servlet-class>LogIn</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogIn</servlet-name>
<url-pattern>/LogIn</url-pattern>
</servlet-mapping>
request.getParameter("txtUserName");
searches for the name = "txtUserName" attribute in input tag and similarly for password field as well, which is not present, modify the JSP code as below and it should work:
<body>
<form method="post" action="LogIn">
<table>
<tr>
<td colspan=2 align="center"
style="font-weight: bold; font-size: 20pt;" align="center"><b>User
Login Form</b></td>
</tr>
<tr>
<td colspan=2></td>
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" id="txtUserName" name="txtUserName"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" id="txtpassword" name="txtpassword" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" id="btnSubmit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
Related
I have a problem with this method, I need to update the value of the "procesado" variable (the variable it starts being from false because its boolean type), I can select one or more checkboxes and when I click the button(Procesar), the value of the variable should change to true.
the problem is when i select the checkboxes that i can't update and display the results
This is my controller:
#RequestMapping(value = "/consulta/{id}", method = RequestMethod.PUT)
public String update(#RequestBody Usuario user, #PathVariable(value="id") List<Usuario> id) {
if(id != null) {
for(Usuario idInt: id) {
Usuario idValor= idInt;
user.setProcesado(true);
iusuarioServices.save(user);
}
}
return "consulta";
}
This is my html
<form action="#{/consulta/{id}}">
<table>
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Procesado</th>
<th>Accion</th>
</tr>
</thead>
<tbody>
<tr th:each="usuarios: ${usuarios}">
<td th:text="${usuarios.id}"></td>
<td th:text="${usuarios.nombre}"></td>
<td th:text="${usuarios.apellido}"></td>
<td th:text="${usuarios.procesado}"></td>
<td><input type="checkbox" th:name="procesado" th:value="${usuarios.id}"></td>
</tr>
</tbody>
<tr>
<td><input type="submit" value="Procesar" /></td>
</tr>
</table>
</form>
enter image description here
This question already has answers here:
request getParameter is always null when using enctype="multipart/form-data"
(2 answers)
Closed 2 years ago.
I have Registration_Page.jsp in that page i had one form ..I want to submit the form to database with the use of the servlet..My servlet name is Save_User_Details.java i want to get the jsp page form data into the servlet..It is getting null value ..How i can get those values in to the servlet anyone help me i am the newer to java..
Registration_Page.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title> Registration Page</title>
<script language="JavaScript" src="gen_validatorv4.js"
type="text/javascript" xml:space="preserve"></script>
</head>
<body>
<h2 style="color: #ff6666" align="center">NEW USER REGISTRATION HERE</h2>
<form ENCTYPE="multipart/form-data" action="Save_User_Details"
method="POST" align="center" name="myform">
<table border="02" align="center" style="color: #ff3399">
<tr>
<th style="color:#ff6666">FIRST NAME:</th>
<td><input type="text" name="FName" id="FName" placeholder="First name" /></td>
</tr>
<tr>
<th style="color:#ff6666">LAST NAME:</th>
<td><input type="text" name="LName" id="LName" placeholder="Last name" /></td>
</tr>
<tr>
<th style="color:#ff6666">PASSWORD:</th>
<td><input type="password" name="password" id="password" placeholder="password"></td>
</tr>
<!-- <tr>
<th>CONFORM PASSWORD:</th>
<td><input type="password" name="conform_password" placeholder="conform password"></td>
</tr> -->
<tr>
<th style="color:#ff6666">EMAIL ID:</th>
<td><input type="email" name="mailid" id="mailid" placeholder="Mailid"></td>
</tr>
<tr>
<th style="color:#ff6666">MOBILE NUMBER</th>
<td><input type="number" name="mobile" id="mobile" placeholder="Mobile number"></td>
</tr>
<tr>
<th style="color:#ff6666">GENDER:</th>
<td style="color:#ff6666"><input type="radio" value="male" name="gender" id="gender"
checked="true">MALE <input type="radio" value="female"
name="gender">FEMALE <input type="radio" value="other"
name="gender">OTHER<br> <br></td>
</tr>
<tr>
<th style="color:#ff6666"><label>DATE OF BIRTH:</label></th>
<td><input type="date" name="dob" id="dob"><br> <br></td>
</tr>
<tr>
<th style="color:#ff6666">COUNTRY:</th>
<td><select name="Country" id="country" style="width: 150px">
<option value="000" selected="selected">[choose yours]</option>
<!-- <select name="country" style="width:150px"> -->
<option>India</option>
<option>USA</option>
<option>UK</option>
<option>Other</option>
</select>
<tr>
<th></th>
<td><input type="submit" style="color: #ff3399" value="SUMIT" />
<button style="color: #ff3399" type="reset">CLEAR</button>
</td>
</tr>
</tr>
<tr>
<td></td>
<td>LOGIN PAGE</td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/javascript"
xml:space="preserve">
//<![CDATA[
//You should create the validator only after the definition of the HTML form
var frmvalidator = new Validator("myform");
frmvalidator.addValidation("FName", "req",
"Please enter your First Name");
frmvalidator.addValidation("FName", "maxlen=20",
"Max length for FirstName is 20");
frmvalidator.addValidation("FName", "minlen=4",
"Min length for FirstName is 4");
frmvalidator.addValidation("FName", "alpha", "Alphabetic chars only");
frmvalidator.addValidation("LName", "req",
"Please enter your Last Name");
frmvalidator.addValidation("LName", "maxlen=20", "Max length is 20");
frmvalidator.addValidation("LName", "minlen=4",
"Min length for LastName is 4");
frmvalidator.addValidation("password", "maxlen=10");
frmvalidator.addValidation("password", "req");
frmvalidator.addValidation("password", "");
frmvalidator.addValidation("password", "minlen=6",
"Min length for Password is 6");
frmvalidator.addValidation("mailid", "maxlen=50");
frmvalidator.addValidation("mailid", "req");
frmvalidator.addValidation("mailid", "email");
frmvalidator.addValidation("mailid", "minlen=6",
"Min length for Mailid is 6");
frmvalidator.addValidation("gender", "dontselect=000");
frmvalidator.addValidation("gender", "req");
frmvalidator.addValidation("mobile", "maxlen=10");
frmvalidator.addValidation("mobile", "numeric");
frmvalidator.addValidation("mobile", "req");
frmvalidator.addValidation("mobile", "minlen=10",
"Mobile number should be 10 numbers");
frmvalidator.addValidation("dob", "maxlen=40");
frmvalidator.addValidation("dob", "req");
frmvalidator.addValidation("Country", "dontselect=000");
frmvalidator.addValidation("Country", "req");
//]]>
</script>
</body>
</html>
Save_User_Details.java
package com.ih.Control;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
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 com.ih.Dao.UserDao;
import com.ih.Model.User_Details;
/**
* Servlet implementation class Save_User_Details
*/
#WebServlet("/Save_User_Details")
public class Save_User_Details extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Save_User_Details() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
User_Details ud=new User_Details();
System.out.println("i am in Save_User_Details..........");
PrintWriter out=response.getWriter();
int status=0;
String fname=request.getParameter("FName");
System.out.println("The FNAME::"+fname);
String lname=request.getParameter("LName");
System.out.println("The LNAME::"+lname);
String password=request.getParameter("password");
System.out.println("The PASSWORD::"+password);
String mailid=request.getParameter("mailid");
String mobile=request.getParameter("mobile");
String gender=request.getParameter("gender");
String dob=request.getParameter("dob");
String country=request.getParameter("country");
System.out.println("FNAME::"+fname+"LNAME::"+lname+"PASSWORD::"+password+"MAILID::"+mailid+"MOBILE::"+mobile+"GENDER::"+gender+"DOB::"+dob+"COUNTRY::"+country);
ud.setFName(fname);
ud.setLName(lname);
ud.setPassword(password);
ud.setMailid(mailid);
ud.setMobile(mobile);
ud.setGender(gender);
ud.setDob(dob);
ud.setCountry(country);
try {
UserDao.SaveRegistration_Details();
RequestDispatcher rd=request.getRequestDispatcher("Login.jsp");
rd.forward(request, response);
} catch (SQLException e) {
out.println("<h1 style='color:red'>FAILED TO INSERT USER DATA TRY AGAIN!!</h1>");
out.println("<a href='Registration_Page.jsp'>REGISTER AGAIN</a>");
e.printStackTrace();
}
}
}
enctype="multipart/form-data" is for 'file upload', not for normal textbox.
When you use enctype="multipart/form-data" form fields aren't available as parameter of the request, so you will get request.getParameter() as null.
You can find more info in HTML5 specification.
I have a couple of JSPs: home and graph.
I am calling a servlet from the home JSP using ajax: ExcelServlet.
I need to forward from the servlet to the graph JSP.
I am able to get control to the Servlet, so the mapping and the ajax calls are fine.
However this is not happening.
home.jsp:
$.ajax({
type: "get",
url: "CopyServlet",
beforeSend:function(){
$('#text3').css({"color":"red"});
$('#text3').text("Running Dynamic Diff Graph");
},
success:function(responseText){
$('#text3').css({"color":"green"});
$('#text3').text(responseText);
},
complete:function(){
$.ajax({
type: "post",
url: "ExcelServlet",
beforeSend:function(){
$('#text4').css({"color":"red"});
$('#text4').text("Preparing Difference Summary");
},
success:function(data){}
});
}
});
Excelservlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ExcelExtract excelextract = new ExcelExtract();
excelextract.extract();
String newDup=excelextract.getNewDup();
request.setAttribute("newDup", newDup);
String diff=excelextract.getDiff();
request.setAttribute("diff", diff);
String newRecNA=excelextract.getNewRecNA();
request.setAttribute("newRecNA", newRecNA);
String oldRecNA=excelextract.getOldRecNA();
request.setAttribute("oldRecNA", oldRecNA);
String unchangedRec=excelextract.getUnchangedRec();
request.setAttribute("unchangedRec", unchangedRec);
if (diff.equals("0.0")){
request.setAttribute("message", "Test Passed: 0 differences");
}
else{
request.setAttribute("message", "Test Failed: Differences found");
}
request.getRequestDispatcher("graph.jsp").forward(request, response);
}
graph.jsp
<body>
<div align="center">
<h1>Welcome to ETL Automation Testing Tool</h1>
<br>
<table border="1">
<th align="center"><b>Difference summary:</b></th>
<tr><td>${oldFile}</td></tr>
<tr><td>${newFile}</td></tr>
<tr><td>${dml}</td></tr>
<tr align="center"><td><b>Counts:</b></td></tr>
<tr>
<td>Old Records:</td>
<td>${oldRec}</td>
</tr>
<tr>
<td>Old Duplicates:</td>
<td>${oldDup}</td>
</tr>
<tr>
<td>New Records:</td>
<td>${newRec}</td>
</tr>
<tr>
<td>New Duplicates:</td>
<td>${newDup}</td>
</tr>
<tr>
<td>Differences:</td>
<td>${diff}</td>
</tr>
<tr>
<td>New Records not in old(Adds):</td>
<td>${newRecNA}</td>
</tr>
<tr>
<td>Old Records not in new(Deletes):</td>
<td>${oldRecNA}</td>
</tr>
<tr>
<td>Unchanged Records:</td>
<td>${unchangedRec}</td>
</tr>
</table>
<h3>${message}</h3>
</div>
</body>
I have a JTextPane with a HyperLinkListener
m_textPane.addHyperlinkListener(new HyperlinkListener()
{
#Override
public void hyperlinkUpdate(HyperlinkEvent hyperlinkevent)
{
EventType eventType = hyperlinkevent.getEventType();
if (eventType == HyperlinkEvent.EventType.ACTIVATED)
{
URL url = hyperlinkevent.getURL();
hyperLinkClicked(hyperlinkevent);
}
}
});
The JTextPant is created with HTML and in this HTML file I have two Links.
<tr>
<td valign="top" class="label">Telefon:</td>
<td class="value">
<a href="telnet:[PhoneNumber.primary.number]">
[PhoneNumber.primary.number]
</a>
</td>
</tr>
<tr>
<td valign="top" class="label">Mobil:</td>
<td class="value">[PhoneNumber:Mobil.number]</td>
</tr>
<tr>
<td valign="top" class="label">Arbete:</td>
<td class="value">[PhoneNumber:Arbete.number]</td>
</tr>
<tr>
<td valign="top" class="label">E-post:</td>
<td class="value">
<a href="mailto:[Email.primary.address|]">
[Email.primary.address|]
</a>
</td>
</tr>
</table>
There is no problem getting the mailto protocol, returns "mailto" but the url for the telnet returns null
Any ideas? If any more information is needed tell me :)
Try to use hyperlinkevent.getDescription(); instead of hyperlinkevent.getURL();
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
String description = e.getDescription();
...
}
}
In my java application i am uploading multiple files using java servlet.
All things works fine until i added extra text field in my form.
I am getting null document when i add text field.
Here is my code:-
JSP Form:-
<form action="upload/servlet" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Upload File: </td>
<td><input type="file" name="file" multiple/>
</td>
<td style="color: red; font-style: italic;"><form:errors
path="file" />
</td>
</tr>
<tr>
<td>Generate Key</td><td> </td>
<td><input type="button" value="Change Key"/>
</td>
<td>${key}</td>
</tr>
<tr>
<td>Zip Code</td><td> </td>
<td><input type="text" value="100001" name="zipcode"/>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Upload" />
</td>
<td> </td>
</tr>
</table>
</form>
Here is my servlet:-
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (!ServletFileUpload.isMultipartContent(req)) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,"Multipart content expected!");
}
ModelMap model = new ModelMap();
try {
#SuppressWarnings("unchecked")
List<FileItem> files = this.upload.parseRequest(req);
String userName=req.getSession().getAttribute("username").toString();
String fileName;
String contentType;
byte[] content;
System.out.print("Zipcode is "+req.getParameter("zipcode"));
for(FileItem item : files) {
if(item.isFormField()){
fileName = item.getName();
contentType = item.getContentType();
content = item.get();
String id=this.indexDocument(fileName, contentType, content,userName,req);
model.put(id, fileName);
System.out.println("Done for "+fileName+ " id "+id);
}
}
} catch (FileUploadException e) {
System.out.println("Error FileUploadException: "+e.getMessage());
throw new ServletException(e);
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error "+e.getMessage());
}
req.setAttribute("message", model);
req.getSession().setAttribute("tmpRetMessage", model);
// RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/success.do");
//dispatcher.forward(req, resp);
resp.sendRedirect("../success.do");
}
If i add two text fields then getting null document error two times. If i add three times then getting error three times.
Here form enctype type multipart (enctype="multipart/form-data"). So request.getParameter() value will be null. So you need to process file field and regular fields means other than file like text, radio, etc separately.
see for more how to get request parameters