开发者

how to generate sequence of unique id in jsp starting from 1000

开发者 https://www.devze.com 2023-02-26 11:06 出处:网络
i want c开发者_如何学Code in jsp to generate unique id starting from 1000. Which data type can i use for it, and how to go about it. Can anyone please giude me...... If you want to generate a random i

i want c开发者_如何学Code in jsp to generate unique id starting from 1000. Which data type can i use for it, and how to go about it. Can anyone please giude me......


If you want to generate a random integer within a certain range , you can use the following snippets :

public int generateRandomNumber(int start, int end ){
    Random random = new Random();
    long fraction = (long) ((end - start + 1 ) * random.nextDouble());
    return ((int)(fraction + start));
}

For example , to get a random integer within 1000 and 8888 , you can call generateRandomNumber(1000, 8888);

If you want to write all the java code inside a JSP (tough I don't suggest this approach too ) , you can create a JSP page like this .You can get a random integer after every refresh.

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Random"%>
<%!
    public int generateRandomNumber(int start, int end ){
        Random random = new Random();
        long fraction = (long) ((end - start + 1 ) * random.nextDouble());
        return ((int)(fraction + start));
    }
%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>TEST RANDOM NUMBER</title>
    </head>
    <body>
         <h1>Generate Random Number:<%=generateRandomNumber(1000,8888)%></h1>
    </body>
    </html>


You can use java.util.Random, Use nextInt() method and simply add 1000 to it, if you want it to be starting from 1000 , you can simply take first no as 1000

Also See

  • how-to-get-a-random-number-in-jstl


These answers all talk about Random numbers - the OP is asking about unique numbers. Using Random numbers, you still get the possibility of duplicates ( although that chance is admittedly small )

An easy way to get a unique number would be just to have a class that has one static synchronised method that increments a statically declared counter and returns it. Seed the counter to start at 1000.

I would implement this in a java class, rather than JSP. As Jogar points out earlier, putting raw java in a JSP can soon get out of hand.

If your application may end up running on more than one JVM ( such as in an application server cluster ) and the number needs to unique across the entire cluster, then this solution won't work. You'll need to use an external counter, such as the database based solution mentioned above.


you can use AUT_INCREMENT feature of db. mysql db to generate unique id .

   create table `TableName`( 
   `v` int UNSIGNED NOT NULL AUTO_INCREMENT , 
   PRIMARY KEY (`v`)
 )  Engine='Default' auto_increment=1000 comment='' row_format=Default  
0

精彩评论

暂无评论...
验证码 换一张
取 消