java判断request.getParameter("Id")为合法数字,而不包含其他除过数字的字符

本文介绍了一种使用Java来检查字符串是否完全由数字组成的简便方法。通过利用正则表达式替换所有数字字符,并检查替换后的字符串是否为空来实现这一目标。

java如何判断一个字符串是纯数字

可以将字符串的数字全部替换掉,然后判断替换后的与”“判断

 

!id.replaceAll("//d", "").equals("")

package com.cissst.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.cissst.entity.Admin; import com.cissst.entity.CustomAccount; import com.cissst.service.IAdminService; import com.cissst.service.ICustomAccountService; import com.cissst.service.impl.AdminServiceImpl; import com.cissst.service.impl.CustomAccountServiceImpl; import com.cissst.util.MD5Util; public class UserServlet extends HttpServlet{ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String action = request.getParameter("action"); IAdminService as = new AdminServiceImpl(); ICustomAccountService cs = new CustomAccountServiceImpl(); HttpSession session = request.getSession(); if("login".equals(action)){ String name = request.getParameter("username"); String password = MD5Util.encode(request.getParameter("password")); String usertype = request.getParameter("usertype"); Admin a = as.findBynp(name, password); CustomAccount c = cs.findBynp(name, password); if(a != null){ String n = a.getName(); String p = a.getPassword(); if(n.equals(name) && p.equals(password)&&"admin".equals(usertype)){ session.setAttribute("admin", a); response.sendRedirect("index.jsp"); }else{ response.getWriter().write("<script charset='UTF-8'>alert(\"用户名或密码错误!\");" + "location.href='index.jsp';</script>"); } }else if(c != null){ String n = c.getUsername(); String p = c.getPassword(); if(n.equals(name) && p.equals(password)&&"user".equals(usertype)){ session.setAttribute("custom_Account", c); response.sendRedirect("index2.jsp"); }else{ response.getWriter().write("<script charset='UTF-8'>alert(\"用户名或密码错误!\");" + "location.href='index.jsp';</script>"); } }else{ response.getWriter().write("<script charset='UTF-8'>alert(\"用户名或密码错误!\");" + "location.href='index.jsp';</script>"); } }else if("logout".equals(action)){ session.invalidate(); //if (request.getSession(false)==null) System.out.println(123); response.sendRedirect("login.jsp"); }else if("relogin".equals(action)){ session.invalidate(); response.sendRedirect("login.jsp"); } } }
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值