HttpServletResponse response;//具体怎么得到这个对象的你应该知道吧
response.setCharacterEncoding("utf-8")
response.setContentType("text/html")
PrintWriter writer =response.getWriter()
writer.write("<a>注册成功</a>")//就如这样把你要的html代码写进去就行了
//
//最后记得这一下
writer.flush()
在html中加入java代码,就是jsp页面,jsp里可以加入java代码了,当然也可以调用外部的java。jsp页面要想运行必须放到容器里,例如最流行的tomcat。你应该是新手,还不明白概念,所以在网上找些视频教程看吧,搜索j2ee或者jsp的教程,jsp就是用java程序动态生成html页面,浏览器不能解析java代码,所以tomcat这种服务器负责解析,生成最终的html
不知兄台是不是说的这种格式的,写一个java文件(servlet),运行后输出一个网页,下面是一个登录界面,你只需要创建一个servlet,然后将其中的doget换成如下代码,将dopost改成doget();即可运行。望采纳!protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String cookieName = "userName"
String cookiePwd = "pwd"
Cookie[] cookies = request.getCookies()
String userName = ""
String pwd = ""
boolean isChecked = false
//如果cookie数组不为null,说明曾经设置过,也就是曾经登录过,那么取出上次登录的用户名和密码
if (cookies != null)
{
for (int i=0i<cookies.lengthi++)
{
Cookie cookie = cookies[i]
if (cookieName.equals(cookie.getName()))
{
userName = cookie.getValue()
}
if (cookiePwd.equals(cookie.getName()))
{
pwd = cookie.getValue()
}
if ("Check".equals(cookie.getName()))
{
isChecked = cookie.getValue().equals("yes")? true : false
}
}
}
response.setContentType("text/htmlcharset=GBK")
request.setCharacterEncoding("gbk")
PrintWriter out = response.getWriter()
String docType = "<DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
String title = "ServletLoginDemo"
out.println(docType + "<html>" + "<head>"
+"<meta http-equiv=\"Content-Type\" content=\"text/htmlcharset=gbk\">"
+ "<style type=\"text/css\">"
+ ".textsize {width: 150px background: #B1DC87}"
+ "</style>"
+ "<title>" + title
+ "</title></head>" + "<body bgcolor=\"#FDF5E6\">"
+ "<br />"
+ "<br />"
+ "<center>" + "<h1>Cookie登陆Demo</h1>"
+ "<hr />"
+ "<form action=\"CookieTest\" method=\"post\">"
+ (userName == null ? "用户名:<input class=\"textsize\" type=\"text\" name=\"UserName\" width=\"20\"></input><br />" : "用户名:<input class=\"textsize\" type=\"text\" name=\"UserName\" width=\"20\" value=" + userName +"></input><br />")
+ (pwd == null ? "密 码:<input class=\"textsize\" type=\"password\" name=\"Password\"></input><br />" : "密 码:<input class=\"textsize\" type=\"password\" name=\"Password\" value=" + pwd +"></input><br />")
+ (isChecked ? "<input type=\"checkbox\" name=\"Check\" checked=\"true\">记住密码</input>" : "<input type=\"checkbox\" name=\"Check\">记住密码</input>")
+ "<center><input type=\"submit\" value=\"登陆\"></center>"
+ "</form>"
+"</center></body></html>")
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)