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>")
}
import java.io.BufferedReaderimport java.io.IOException
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
/**
* @file:GoToWEB.java
* @author death_loong
*
*/
public class GoToWEB {
public static void main(String args[]) throws Exception{
HttpURLConnection httpURLConnection =null
try {
httpURLConnection =(HttpURLConnection) new URL("http://www.baidu.com").openConnection()
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
BufferedReader reader =new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))
String temp=null
while((temp=reader.readLine())!=null){
System.out.println(temp)
}
}
}
它得到的只是网页的源文件,不知道你要的是不是这样的
可以用:File f_html = new File("Login.html")f_html.createNewFile()
要想生成html页面的话,容器会替我们直接把jsp编译成servlet输出成html静态页面进行展示。
你要像手动输出html的展示内容可以自己写一个servlet,使用output方法输出html标签代码段直接打印到客户端。
还有如果你想写入html文件的话,你可以通过fileinput字节写入。(这种写法servlet教程上很多实例,包括如何生成文件,如何通过字节或者字符流的形式写入和保存)