2014年5月17日星期六

jsp java code snippet in how to receive the output of the label in the servlet ?

junior inquire . .
like this :
jsp in :
<%
out.println ("");
%>
servlet in :
String name = request.getParameter ("userName");
get the value of less than userName .
because I have some special judge, so the output of some form form in java code snippet , how to receive their values ​​?
------ Solution ---------------------------------------- ----
when the form is submitted , the background can be based on input name = "userName" value , do not need to write the script. You certainly get that way writing can not.
------ For reference only -------------------------------------- -


how to obtain it . .
------ For reference only -------------------------------------- -
jsp code is our teacher gave , servlet functionality did not realize that this issue get me one night. .
------ For reference only -------------------------------------- -
want to achieve what effect , to write java code in jsp dom is generated norms
really wanted to write , with such
<%
out.println ("");
%>
then click on the input button is where the form can be submitted to the corresponding servlet in
------ For reference only ---------------- -----------------------

not see your userName field has a value
------ For reference only ---------------------------------------

did not see your domain has a value of userName  


I mean, his page has when the form is submitted to the servlet , servlet can get the value of userName attribute name based on input text box

request.getParameter ("userName");
------ For reference only ------------------------ ---------------


tried, looks or not.
<%
for (customer cus: clist) {
out.println ("");
if (! cus.getCustomerID (). equals (session.getAttribute ("editid" ;) ) ) {
out.println ("" + cus.getCustomerID () + "");
out.println ("" + cus.getName () + "");
out.println ("" + cus.getPhone () + "");
out.println (" Delete ")?;
out.println (" Edit ");
} else {
out.println (" "); ;
out.println ("" + cus.getCustomerID () + "");
out.println (" ");
out.println (" ");
out.println (" OK ")?;
out.println (" Cancel ");
}
out.println ("");

}
%>
just want to achieve edit, then OK, the servlet can not receive text into the cname and cphone
------ For reference only ------------- --------------------------

did not see the value of your domain has userName          
  
  
I mean, his page has when the form is submitted to the servlet , servlet can get the value of userName attribute name based on input text box   
  
request.getParameter ("userName");  
yes ah, the problem is the code you write is an empty string , ah, the resulting string is empty , is it null? If you submit null description form field is not userName field
------ For reference only --------------------------------------- < br>
  
tried, looks or not.   
<%   
for (customer cus: clist) {   
out.println ("");   
if (! cus.getCustomerID (). equals (session.getAttribute ("editid" ;) ) ) {   
out.println ("" + cus.getCustomerID () + "");   
out.println ("" + cus.getName () + "");   
out.println ("" + cus.getPhone () + "");   
out.println (" Delete ")?;   
out.println (" Edit ");   
} else {   
out.println (" "); ;   
out.println ("" + cus.getCustomerID () + "");   
out.println (" ");   
out.println (" ");   
out.println (" OK ")?;   
out.println (" Cancel ");   
}   
out.println ("");   
  
}   
%>   
just want to achieve edit, then OK, the servlet can not receive text into the cname and cphone  

out.println (" OK ");
------ For reference only ---- -----------------------------------

  
how to obtain it . .  

I wrote a simple example
<%
String path = request.getContextPath ();
String basePath = request.getScheme () + ":/ /" + request.getServerName () + ":" + request.getServerPort () + path + "/" ;
%>
The code above is jsp page has the web project , the project is to get the script path , do not need to write .
just fine.
here requestServlet is servlet name.
------ For reference only -------------------------------------- -

    
tried, looks or not.     
<%     
for (customer cus: clist) {     
out.println ("");     
if (! cus.getCustomerID (). equals (session.getAttribute ("editid" ;) ) ) {     
out.println ("" + cus.getCustomerID () + "");     
out.println ("" + cus.getName () + "");     
out.println ("" + cus.getPhone () + "");     
out.println (" Delete ")?;     
out.println (" Edit ");     
} else {     
out.println (" "); ;     
out.println ("" + cus.getCustomerID () + "");     
out.println (" ");     
out.println (" ");     
out.println (" OK ")?;     
out.println (" Cancel ");     
}     
out.println ("");     
    
}     
%>     
just want to achieve edit, then OK, the servlet can not receive text into the cname and cphone          
  
out.println (" OK ");  
as the original value is passed , it is now edit function is to modify cus.name and cus.phone
------ For reference only ----------- ----------------------------

did not see the value of your domain has userName                
    
    
I mean, his page has when the form is submitted to the servlet , servlet can get the value of userName attribute name based on input text box     
    
request.getParameter ("userName");          
yes ah, the problem is the code you write is an empty string , ah, the resulting string is empty , is it null? If you submit null description does not form userName field domain  


confuse me , you are still the person who asked the respondents , value if you do not fill in the text box userName , the page is not enough to give a hint , can not be empty .
generally need to deposit something into the database if it can not be empty , so you can do to control this process , the input value must be ah.

irregular words , if you do not enter a value , I have to be a non-empty servlet judgment does not like,
servlet:
String userName = request.getParameter ("userName");
if (userName! = null && userName! = "") {
}


------ For reference only ---------------------------------- -----

did not see the value of your domain has userName                
    
    
I mean, his page has when the form is submitted to the servlet , servlet can get the value of userName attribute name based on input text box     
    
request.getParameter ("userName");          
yes ah, the problem is the code you write is an empty string , ah, the resulting string is empty , is it null? If you submit null description does not form userName field domain  

Field General page to enter the ( database ) can not be an empty field, if you do not enter on the form is submitted , that I want you to do this procedure there is no meaning.
------ For reference only ------------------------------ ---------

    
how to obtain it . .          
  
I wrote a simple example   
<%   
String path = request.getContextPath ();   
String basePath = request.getScheme () + ":/ /" + request.getServerName () + ":" + request.getServerPort () + path + "/" ;   
%>   
The code above is jsp page has the web project , the project is to get the script path , do not need to write .   
just fine.   
here requestServlet is servlet name.  

first thank you ! First, forgive me for not really understand why this writing , and I still have not changed this effect , the value of the console output of the receiver or null. .
------ For reference only -------------------------------------- -

      
tried, looks or not.       
<%       
for (customer cus: clist) {       
out.println ("");       
if (! cus.getCustomerID (). equals (session.getAttribute ("editid" ;) ) ) {       
out.println ("" + cus.getCustomerID () + "");       
out.println ("" + cus.getName () + "");       
out.println ("" + cus.getPhone () + "");       
out.println (" Delete ")?;       
out.println (" Edit ");       
} else {       
out.println (" "); ;       
out.println ("" + cus.getCustomerID () + "");       
out.println (" ");       
out.println (" ");       
out.println (" OK ")?;       
out.println (" Cancel ");       
}       
out.println ("");       
      
}       
%>       
just want to achieve edit, then OK, the servlet can not receive text into the cname and cphone                
    
out.println (" OK ");          
as the original value is passed , it is now edit function is to modify cus.name and cus.phone  
you honestly write post form bars


<%
for (customer cus: clist) {
out.println ("");
if (! cus.getCustomerID (). equals (session.getAttribute ("editid" ;) ) ) {
out.println ("" + cus.getCustomerID () + "");
out.println ("" + cus.getName () + "");
out.println ("" + cus.getPhone () + "");
out.println (" Delete ")?;
out.println (" Edit ");
} else {
out.println (" "); ;
out.println ("" + cus.getCustomerID () + "");
out.println (" ");
out.println (" ");
out.println (" OK ");
out.println (" Cancel ");
}
out.println ("");

}
%>
------ For reference only ------------- --------------------------

      
how to obtain it . .                
    
I wrote a simple example     
<%     
String path = request.getContextPath ();     
String basePath = request.getScheme () + ":/ /" + request.getServerName () + ":" + request.getServerPort () + path + "/" ;     
%>     
The code above is jsp page has the web project , the project is to get the script path , do not need to write .     
just fine.     
here requestServlet is servlet name.          
  
first thank you ! First, forgive me for not really understand why this writing , and I still have not changed this effect , the value of the console output of the receiver or null. .  

way my form submitted post Oh , then call the servlet 's doPost () method , oh , oh not tune doGet method . You send your files and servlet jsp page gave me a look , I know. I say that certainly can pass values ​​
------ For reference only --------------------------- ------------

        
how to obtain it . .                      
      
I wrote a simple example       
<%       
String path = request.getContextPath ();       
String basePath = request.getScheme () + ":/ /" + request.getServerName () + ":" + request.getServerPort () + path + "/" ;       
%>       
The code above is jsp page has the web project , the project is to get the script path , do not need to write .       
just fine.       
here requestServlet is servlet name.                
    
first thank you ! First, forgive me for not really understand why this writing , and I still have not changed this effect , the value of the console output of the receiver or null. .          
  
way my form submitted post Oh , then call the servlet 's doPost () method , oh , oh not tune doGet method . You send your files and servlet jsp page gave me a look , I know. I say that certainly can pass values  

package Ex6;

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 Ex6_2Servlet extends HttpServlet {


public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType ("text / html");
PrintWriter out = response.getWriter ();
doPost (request, response);
out.close ();
}


public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType ("text / html");
PrintWriter out = response.getWriter ();
customerDao dao = new customerDao ();
String opttype = request.getParameter ("opttype");
String customerID = request.getParameter ("customerID");
String name = request.getParameter ("name");
String phone = request.getParameter ("phone");
if (customerID! = null) {
customer c = new customer ();
c.setCustomerID (customerID);
c.setName (name);
c.setPhone (phone);
dao.addCustomer (c);
}

if ("delete". equals (opttype)) {
String cid = request.getParameter ("cid");
if (cid! = null) {
dao.deleteCustomer (cid);
}
}
if ("ok". equals (opttype)) {
String cid = request.getParameter ("cid");
String cname = request.getParameter ("cname");
String cphone = request.getParameter ("cphone");
if (cid! = null && cname! = null && cphone! = null) {
customer cus = new customer ();
cus.setCustomerID (cid);
cus.setName (cname);
cus.setPhone (cphone);
dao.updateCustomer (cus);
System.out.println ("update" + cid + cname + cphone);
}
request.getSession () removeAttribute ("editid").;
System.out.println ("null" + cid + cname + cphone);
}
if ("edit". equals (opttype)) {
String editid = request.getParameter ("cid");
request.getSession () setAttribute ("editid", editid).;
}
if ("cancel". equals (opttype)) {
request.getSession () removeAttribute ("editid").;
}
response.sendRedirect ("Ex6_2.jsp");
out.close ();
}

}


<%
%>           .                                              
<%                           
%>                                                        .                                                                                              
}                                                         
}                  
}   
}                                    
}                     
}         
}         
}      
}            
<%         
%>                                                                                                                                                    .                                              
<%                           
%>                                                        .                                                                                              
}                                                         
}                  
}   
}                                    
}                     
}         
}         
}      
}            
<%         
%>                                                                                                                                          ? ? ?                  
<%                                                                                                                                                                                                                                                             
<%                                                               .                      
<%                                                                                                                                                                                                                                                                                                                                 
<%                                                                                                                    .      ? ? ? . .                          
<%                                                                                                                                                                                                                                                                                                                                                                                                     
<%                                                                                                                                                                      .                  .

没有评论:

发表评论