Home  >  Article  >  Computer Tutorials  >  Use JSP to write a character statistics program that requires users to enter strings for statistics

Use JSP to write a character statistics program that requires users to enter strings for statistics

WBOY
WBOYforward
2024-01-22 14:54:161235browse

Use JSP to write a character statistics program that requires users to enter strings for statistics

Write a word count program using JSP language. The program requires the user to input string 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)

}

Write guessing numbers in jsp

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)

Please enter a number from 1-100:

Please write a JSP page, enter a number into the JSP page, click the button to submit the page

Input interface:

Demo
Please enter a number:

Processing, output interface:

You entered an even number

You entered an odd number

## JSP text input number problem

It’s very simple with javascript

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!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete
Previous article:cmd command listNext article:cmd command list