Sending email from a form - java

I have one problem in the below code first:
<form action="resultPage.jsp" >
<br/>
<br/>
<br/>
<h4>
<label>subject</label> <input name="subject" type="text" value="subject" />
</h4>
<textarea rows="7" cols="40" name="comment" >comment</textarea>
</br>
<input type="submit"/>
</form>
Then I write the below code:
<%
MailManager mailManager = new MailManager();
String subject= request.getParameter("subject");
String comment=request.getParameter("comment");
if((request.getParameter("subject")!=null) && (request.getParameter("comment")!=null) )
mailManager.sendMessage("send E-mail addrs","password","smtp.gmail.com","465", new String[]{"receive addrs"}, subject,comment, "text/html");
%>
But I can't send or receive any e-mail.
When I write mailManager.sendMessage() without if statement I give an exception
java.lang.NullPointerException

Clearly, the fact that leaving out the if cause a NullPointerException means that one or both of the comment or subject parameters is missing.
That would suggest that the problem is in the form.
Use your browser's web developer capabilities to find out precisely what request is being sent to your server. It looks like it would be a GET request, so the parameters should be in the request URL.

Hey guys I found solution reason of exception was that object of request.getParameter(t1) been not made, so there was a pointer pointing to null object
For preventing this error we can use below code :
<%
String str=request.getParameter("t1");
if (str==null )
str=0+"" ;
int i=Integer.parseInt(str);
out.print(i);
%>

Related

JSP pass hidden input value to servlet when the page load

I should be using getRemoteUser functionality to get the logged in user. Until the authentication part get created I am trying to hard code the user in the jsp page and pass that in the my servlet. But when I try to print out the value its null:
<input type="hidden" name="userId" id="userId" value="123456789" />
Here is how I tried to get the user:
String fakeUser = request.getParameter("userId");
PrintWriter out = response.getWriter();
out.println(fakeUser);
System.out.println(fakeUser)
I also tried the solution mentioned following Stackoverflow post but that didn't work either.
passing value from jsp to servlet
As you are trying to use hidden-form field I assume that you are trying to do some sort of state management.
try something like this
<form action="urlOfYourServlet" method="post">
Enter your name : <input type ="text" name = "name">
<input type="hidden" name="hidden" value="Welcome">
<input type="submit" value="submit">
</form>
In servlet
String getHiddenValue=request.getParameter("hidden");
String name=request.getParameter("name");
System.out.println(name+" Hidden field Value is :"+getHiddenValue);
Disadvantage :
Only textual information can be persisted between request.
This method works only when the request is submitted through input form
Instead try url-redirecting or HttpSession

implementation of post redirect get in java web

am i implementing the post redirect get approach in java web. i have this index.jsp which i can add information to database.
<form action="servlet" method="post>
<input type="text" placeholder="itemname"/>
<input type="text" placeholder="itemprice"/>
<input type="submit" value="add item"/>
</form>
and in servlet i process the username and password
//returns a boolean if success or not
if(processItem(itemname,itemprice)){
response.sendRedirect("secondservlet?ADD=success");
}
and in the secondservlet
if(request.getParameter("ADD").equals("SUCCESS"))
request.getRequestDispatcher("success.jsp").forward(request,response);
am i doing it right?
am i doing it right?
You are sending a POST, then redirecting, which results in a GET. If that is what you were trying to do, then yes, you are doing it right.
Note that your second servlet should probably check if getParameter(..) returns null. You might get to the second servlet from some other call that doesn't include any request parameters.

How to send label parameter from JSP to servlet?

I have a jQuery dialogue box which contains values as checkboxes. On selecting the checkboxes I am storing the selected values into label. Next I have to send these values from label as parameter through form to servlet but I don't know how to complete it.
Here is my code:
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date" size="4">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/>
Select Reporting Level
<label class="button2" style="display:none" id="depart"> Department</label>
</form>
I am retrieving these parameters in my Servlet as:
String reportname=request.getParameter("depart");
System.out.println(reportname);
But it is returning null values. Please help me.
Thanks in advance.
You have to use hidden input field:
<input type="hidden" name="depart" />
You need to understand what gets passed on form submission and what is not. In a nutshell, only values of the input fields get sent to the server. You have several ways to solve your problem:
Write value to a hidden input field
Modify the query string (what gets sent after ? in your GET request) during form submission (using java script):
?...&depart=xxx

How to get form name value in JSP

Is there a way to get the name of the form element itself using JSP? I searched google and did not find any examples which show how to get the name of the form value specified in the JSP file.
For example, I have the below form,
<html>
<form name="register" action="servregister" method="POST"
onsubmit="return validate();">
</form>
</html>
I would like to get the string "register" to a string variable in JSP. Is there a way to do this?
No, a <form> is not submitted with the request. Instead create a hidden input element which holds that information.
<input type="hidden" name="name of form" value="value" />
or possibly the submit element
<input type="submit" name="distinguishing name" value="submit" />
Either of these in a form with be sent as a url-encoded parameter.
There is probably a better solution to what you are trying to do if you explain your goals.
Consider looking into different patterns for achieving your goals.

form values not submitted to the servlet

I am trying to submit the text field value and print it using the servlet. The index.jsp is my main page and I am using jsp:include to include the form which reside in another page which is login.html.
here is the code i have for login.html
<form id="f1" action="ControllerServlet" method="GET">
<p>username
<input class ="text-input" type="text" id="txtusername" />
</p>
<p>
<input type="submit" value="submit" />
</p>
the index.jsp
<div id="col3_content" class="clearfix">
<h1>H1 Heading</h1>
<jsp:include page="login.html"></jsp:include>
</div>
the controller servlet
String usrname = request.getParameter("txtusername").toString();
out.print(usrname);
The problem is this is throwing a null pointer exception. what am I doing wrong here ? any help appreciated. thanks
Please use name not id
<input class ="text-input" type="text" name="txtusername" />
The id is not used to identify the name of the input parameter. The right attribute for the parameter is name, currently you are using an input without a name. So use
<input class ="text-input" type="text" name="txtusername" id="txtusername" />
You need to define name attribute of input tag to get it in Servlet by name.
<input class ="text-input" type="text" id="txtusername" name="txtusername" />
Also make sure you are writing code in doGet or service method of servlet as you have GET as action in form tag.
Code for Login.html
<form action="ControllerServlet" method="GET">
<p>username :
<input type ="text" name="txtusername" /></p>
<p><input type="submit" value="submit" /> </p>
</form>
ControllerServlet.java
public void service(ServletRequest request, ServletResponse response)
{
String username = request.getParameter("txtusername");
PrintWriter out = response.getWriter();
out.println("User Name " + username)
I faced a similar situation, when I checked front end, the form seems to have all the value populated correctly. However, after form.submit, from server side using request.getParameter("the parameter") does not return the value populated. After tuning on the network traffic tab in browser, I see the parameter was there, but there was a typo.
Hopefully could save you some time if same thing happens to you.

Categories