Home >Computer Tutorials >Computer Knowledge >Use JSP to write a character statistics program that requires users to enter strings for statistics
I used regular expressions to help you get it:
Required package: java.util.regex.Pattern;
String str=request.getParameter("str"); //Get the value of that text box, assuming it is str
int numberCount=0; //Number of numbers
int aplha=0; //number of letters
int other=0; //Other number
for(int i=0;i if(Pattern.matches("^[0-9]",str.charAt(i) "")){ numberCount ; } else if(Pattern.matches("^[a-z]||[A-Z]",str.charAt(i) "")){ aplha ; } else{ other ; } response.getWriter.println("number:" numberCount "letter:" aplha "other:" other) } Because there is no control named num defined on your index.jsp page, so request.getParameter("num") is NULL. Change on the index.jsp page to , that is, name=num"changed to name="num"(num is preceded by double quotes), value=""changed to value=""( Just remove the spaces between the double quotes). The complete index.jsp file content is as follows: (guess.jsp file does not need to be modified)Write guessing numbers in jsp
Input interface:
Processing, output interface:
Add
in the middle
<script><p> function test(){<p> var obj = document.getElementById("id").value; // Replace the id here with your own text id<p> if(obj>0 && obj<100){<p> return true;<p> }else<p> alert("cuowu");<p> }<p> </script>In your submit button action="test(); return false;"
Written directly here without testing
You can learn javascript yourself, it’s a very simple thing
The above is the detailed content of Use JSP to write a character statistics program that requires users to enter strings for statistics. For more information, please follow other related articles on the PHP Chinese website!