jsp杂碎

    技术2022-05-11  39

    解决中文无法显示的问题

    1:修改区域设置:在控制面版中选择区域设置,设为英语(美国)?nbsp;

    然后重起。一切就都正常。

    2:在jsp页中加入一条语句:

    <%@ page contentType="text/html;charset=gb2312" %> ?

    ?sp显示就正常了。

    3:在编译servletjsp时加入代码选项。编译servlet使用

    javac -encoding iso8859_1 myservlet.java

    jspzone配置文件中.修改编译参数为:

    compiler=builtin-javac -encoding ISO8859_1

    使用这种方法后,不需要作其他的改动就可以正常显示中文了。

    4:最土的办法,在servlet源程序中加入代码变换语句。如

    try{

    out.println(new ( (new String("hello world")).getBytes("GBK"),"ISO8859_1"))

    }

    catch( UnsupportedEncodingException e)

    {

    .......

    }

    使用这种方法一定要注意捕获UnsupportedEncodingException

     

    页面传地递参数

    post/getreguest.getParameter(“name”);

    request的方法

    字符串函数string

    查找:Indexof() lastindexof()

    截取:substring

     

    跳转页面

    window.location.href="logout.jsp"

     

    application.setAttribute(“name”,”lion”); //设置参数

    Enumeration data = application.getArributeNames();//取得所有变量的名称

    Data.hasMoreElements()//判断是否还有变量

    Data.nextElement()//取得下一个变量

    Application.getAttribute(“name”)//取得变量的值

     

    Config/Out/Page对象

     

    DriverManager

    Class.forName(“org.gjt.mm.mysql.Driver”);  //载入MYSQL驱动程式

    getConnection(String url, String user, String password)  //与数据库建立连接

    getDriver(String url) //打开指定地址的数据库驱动程式

     

    Cookie

    Cookie  classname = new Cookie(String index,String value);//建立Cookie

    Response.addCookie(classname); //存储Cookie

    Cookie[] AllCookies = request.getCookies(); //取得所有的Cookie

    AllCookies[i].getName(); //取得index

    AllCookies[i].getValue();//取得value

    class.setMaxAge(Time); //设置Cookie的使用期限

     

    Session

    HttpSession mysession = request.getSession();

    mysession.setAttribute("user",username);

    String username = (String)mysession.getAttribute("user");

    mysession.invalidate();

     

    转化成汉字

    String ToGB2312(String str)

    {

      Byte GB2312[] = new byte[str.length()];

      for(int i=0; i<str.length(); i++)

      {

         GB2312[i] = (byte)str.charAt(i);

    }

    return new String(GB2312);

    }

    Jsp 指令 <%@taglib uri=”taglibraryURI” prefix=”tagPrefix”%>// 让用户使用第三方标签 <jsp:forward page=”ForwardTo.jsp”><jsp:param name=”username” value=”Scott”/></jsp:forward>

    最新回复(0)